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/07/25 19:15] – code -> gh rootlang:cpp:variable [2025/02/06 10:12] (Version actuelle) – Ajout de "Variable optionnelle" root
Ligne 3: Ligne 3:
  
 {{gh>https://github.com/bansan85/wiki_le_garrec_fr/blob/master/cpp/variable/pointer_function.cpp}} {{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>
 +struct S5 {
 +  template <typename... T, typename... U>
 +  void f(U&&...) const {
 +    std::cout << "S5" << sizeof...(T) << "-" << sizeof...(U) << "\n";
 +  }
 +};
 +
 +auto ptr = static_cast<void (S5::*)(int&&, char&&) const>(&S5::f<int, double, char>)
 +</code>
 +
 +[[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>
 +std::conditional_t<std::is_integral_v<T>, int, void*> field;
 +</code>
  
 ====Programmation fonctionnelle==== ====Programmation fonctionnelle====
  
-===Initialisation complexe d'une variable===+===Initialisation complexe d'une constante===
  
 Plutôt que Plutôt que
Ligne 16: Ligne 39:
 {{gh>https://github.com/bansan85/wiki_le_garrec_fr/blob/master/cpp/variable/const_variable_good.cpp}} {{gh>https://github.com/bansan85/wiki_le_garrec_fr/blob/master/cpp/variable/const_variable_good.cpp}}
  
 +====Erreurs====
 +
 +  * ''non-const lvalue reference to type 'X<...>' cannot bind to a temporary of type 'X<...>%%'%%''
 +
 +Parfois, il n'est pas possible de faire:
 +
 +<code cpp>
 +int i;
 +int& j = i;
 +</code>
 +
 +Il faut alors soit passer par une référence constante :
 +
 +<code cpp>
 +int i;
 +const int& j = i;
 +</code>
 +
 +ou passer par un pointeur :
 +
 +<code cpp>
 +int i;
 +int* j = &i;
 +</code>
 +
 +[[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.1595697351.txt.gz · Dernière modification : 2020/07/25 19:15 de root