Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente |
lang:cpp:variable [2021/05/29 13:01] – [Pointeur sur la méthode d'une classe] : ajout de la source root | lang:cpp:variable [2025/02/06 10:12] (Version actuelle) – Ajout de "Variable optionnelle" root |
---|
struct S5 { | struct S5 { |
template <typename... T, typename... U> | template <typename... T, typename... U> |
void f(U...) const { | void f(U&&...) const { |
std::cout << "S5" << sizeof...(T) << "-" << sizeof...(U) << "\n"; | std::cout << "S5" << sizeof...(T) << "-" << sizeof...(U) << "\n"; |
} | } |
}; | }; |
| |
auto ptr = static_cast<void (S5::*)(int, char) const>(&S5::f<int, double, char>) | auto ptr = static_cast<void (S5::*)(int&&, char&&) const>(&S5::f<int, double, char>) |
</code> | </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}} | [[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==== |
| |
{{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}} |