lang:cpp:templatespecialisation
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
lang:cpp:templatespecialisation [2021/05/13 13:23] – [Il faut commencer par spécialiser le premier template] root | lang:cpp:templatespecialisation [2021/07/05 09:51] (Version actuelle) – Déplacement de Divers en bas root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====Divers==== | ||
- | |||
- | ===Empêcher l' | ||
- | |||
- | '' | ||
- | |||
- | <code cpp> | ||
- | #include < | ||
- | |||
- | template < | ||
- | struct foobar : std:: | ||
- | |||
- | // Template général. | ||
- | template <class T> | ||
- | T fun(T a) { | ||
- | static_assert(foobar< | ||
- | "this function has to be implemented for desired type" | ||
- | } | ||
- | |||
- | template <> | ||
- | int fun(int a) { | ||
- | return a; | ||
- | } | ||
- | |||
- | int main() { | ||
- | // fun< | ||
- | fun< | ||
- | // fun< | ||
- | } | ||
- | </ | ||
- | |||
====Spécialisation partielle==== | ====Spécialisation partielle==== | ||
Ligne 361: | Ligne 330: | ||
[[https:// | [[https:// | ||
+ | |||
+ | ====Divers==== | ||
+ | |||
+ | ===Empêcher l' | ||
+ | |||
+ | '' | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | |||
+ | template < | ||
+ | struct foobar : std:: | ||
+ | |||
+ | // Template général. | ||
+ | template <class T> | ||
+ | T fun(T a) { | ||
+ | // La variable value n'est évaluée que si la méthode est utilisée. | ||
+ | static_assert(foobar< | ||
+ | "this function has to be implemented for desired type" | ||
+ | } | ||
+ | |||
+ | template <> | ||
+ | int fun(int a) { | ||
+ | return a; | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | // fun< | ||
+ | fun< | ||
+ | // fun< | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Autre exemple avec '' | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | |||
+ | struct A | ||
+ | { | ||
+ | using Ty = int; | ||
+ | }; | ||
+ | |||
+ | struct B {}; | ||
+ | |||
+ | // KO, should be OK. | ||
+ | // using Ttrue = std:: | ||
+ | // KO | ||
+ | // using Tfalse = std:: | ||
+ | |||
+ | template < | ||
+ | struct LazyLoadIdentity | ||
+ | { | ||
+ | using type = T; | ||
+ | }; | ||
+ | |||
+ | template < | ||
+ | struct LazyLoadTy : LazyLoadIdentity< | ||
+ | |||
+ | // OK | ||
+ | using Ttrue = std:: | ||
+ | // KO | ||
+ | //using Tfalse = std:: | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
lang/cpp/templatespecialisation.1620905005.txt.gz · Dernière modification : 2021/05/13 13:23 de root