Outils pour utilisateurs

Outils du site


lang:cpp:variable

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
lang:cpp:variable [2020/02/11 23:34] – Ajout de "Initialisation complexe d'une variable constante" rootlang:cpp:variable [2025/02/06 10:12] (Version actuelle) – Ajout de "Variable optionnelle" root
Ligne 1: Ligne 1:
-===Initialisation complexe d'une variable constante===+====Déclaration des variables==== 
 +===Pointeur sur la méthode d'une classe===
  
-Plutôt que+{{gh>https://github.com/bansan85/wiki_le_garrec_fr/blob/master/cpp/variable/pointer_function.cpp}} 
 + 
 +Exemple avec une fonction utilisant des template avec auto déduction. Dans ce cas, il faudra faire un cast pour donner le type des éléments déduits.
  
 <code cpp> <code cpp>
-std::string str; +struct S5 { 
-if (i == 1) +  template <typename... T, typename... U> 
-  str = "un"+  void f(U&&...) const { 
-else if (i = 2) +    std::cout << "S5" << sizeof...(T<< "-<< sizeof...(U<< "\n"; 
-  str = "deux"; +  
-else +}; 
-  str = "trois";+ 
 +auto ptr = static_cast<void (S5::*)(int&&, char&&) const>(&S5::f<int, double, char>)
 </code> </code>
  
-écrire+[[https://stackoverflow.com/questions/17874489/disambiguate-overloaded-member-function-pointer-being-passed-as-template-paramet|Disambiguate overloaded member function pointer being passed as template parameter]] {{ :lang:cpp:variable:c_-_disambiguate_overloaded_member_function_pointer_being_passed_as_template_parameter_-_stack_overflow_2021-05-29_08_00_38_.html |Archive du 26/07/2013 le 29/05/2021}} 
 + 
 +===Variable optionnelle=== 
 + 
 +Création une variable ''field'' de type ''void*'' si ''T'' est de type ''int''.
  
 <code cpp> <code cpp>
-const std::string str = [&i]() +std::conditional_t<std::is_integral_v<T>, int, void*> field;
-+
-  if (i == 1) +
-    return "un"; +
-  else if (i == 2) +
-    return "deux"; +
-  else +
-    return "trois"; +
-}();+
 </code> </code>
  
-===Pointeur sur la méthode d'une classe=== +====Programmation fonctionnelle====
-<code cpp> +
-// La classe +
-class Classe +
-+
-public: +
-  // Pas ici. +
-  const Classe* function(size_t i) const +
-  { return this; } +
-  // Ici. +
-  const Classe* function2(size_t i) const +
-  { return this; } +
-};+
  
-int main() +===Initialisation complexe d'une constante===
-+
-  // Le pointeur de nom ptr. +
-  const Classe* (Classe::*ptr)(size_t i) const &Classe::function2; +
-  Classe C; +
-  (C.*ptr)(2); +
-+
-</code>+
  
-===Connaître le type d'une variable auto à la compilation=== +Plutôt que
-Cela se fait en lisant le message d'erreur à la compilation.+
  
-<code> +{{gh>https://github.com/bansan85/wiki_le_garrec_fr/blob/master/cpp/variable/const_variable_bad.cpp}}
-template<class Type> struct S;+
  
-int main() { +écrire
-   auto x = ...; +
-   S<decltype(x)>(); +
-+
-</code>+
  
-Message d'erreur possible (le type est le template de la structure S) : +{{gh>https://github.com/bansan85/wiki_le_garrec_fr/blob/master/cpp/variable/const_variable_good.cpp}}
-  utilisation du type non défini 'S<const std::list<unsigned char> &>'+
  
-[[https://stackoverflow.com/questions/24441505/retrieving-the-type-of-auto-in-c11-without-executing-the-program|Retrieving the type of auto in C++11 without executing the program]] {{ :lang:cpp:variable:c_-_retrieving_the_type_of_auto_in_c_11_without_executing_the_program_-_stack_overflow_2019-12-18_22_53_20_.html |Archive du 26/06/2014 le 18/12/2019}}+====Erreurs====
  
-===Pointer vers une classe template depuis une classe sans template=== +  * ''non-const lvalue reference to type 'X<...>' cannot bind to a temporary of type 'X<...>%%'%%'' 
-Il faut mettre une interface commune sans template à la classe template.+ 
 +Parfois, il n'est pas possible de faire:
  
 <code cpp> <code cpp>
-struct BaseExample { +int i; 
-    virtual void do_stuff() 0+int& j i
-    virtual ~BaseExample() {} +</code>
-};+
  
-template <typename T> +Il faut alors soit passer par une référence constante :
-struct Example BaseExample { ... };+
  
-// .. +<code cpp> 
-BaseExample *obj;+int i; 
 +const int& j = i; 
 +</code> 
 + 
 +ou passer par un pointeur : 
 + 
 +<code cpp> 
 +int i; 
 +intj = &i;
 </code> </code>
  
-[[https://stackoverflow.com/questions/8205902/how-can-i-declare-a-template-pointer-without-knowing-the-type|How can I declare a template pointer without knowing the type?]] {{ :lang:cpp:variable:c_-_how_can_i_declare_a_template_pointer_without_knowing_the_type_-_stack_overflow_2019-12-18_22_53_28_.html |Archive du 21/11/2011 le 18/12/2019}}+[[https://stackoverflow.com/questions/18565167/non-const-lvalue-references|Non const lvalue references]] {{ :lang:cpp:variable:c_-_non_const_lvalue_references_-_stack_overflow_2021-11-05_23_19_03_.html |Archive du 02/09/2013 le 05/11/2021}}
lang/cpp/variable.1581460452.txt.gz · Dernière modification : 2020/02/11 23:34 de root