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/17 22:07] – [Empêcher l'utilisation du template non spécialisé] : ajout d'un exemple de LazyLoad 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) { | ||
- | // 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:: | ||
- | </ | ||
====Spécialisation partielle==== | ====Spécialisation partielle==== | ||
Ligne 393: | 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.1621282035.txt.gz · Dernière modification : 2021/05/17 22:07 de root