How to use CLASS TEMPLATES, type traits, partial and full class template specialization

  Рет қаралды 1,477

Code for yourself

Code for yourself

Күн бұрын

Class templates with the ability to partially and fully specialize them are arguably what makes C++ so powerful! We cover most of what one needs to know about what class templates are, how to specialize them fully or partially as well as how it all plays its role if we want to implement type traits.
Before you watch this video I recommend you to watch these videos if you haven't already:
why use templates: • Why use templates in m...
what templates do under the hood: • Templates under the hood
how to use function templates: • How to use FUNCTION TE...
In this video we look at
Class method templates
Class templates
How they can be specialized partially and fully
And as always some easy to follow examples as well as stories about what can go wrong with template specialization
We cover a lot of ground in this video, and it is quite long as much as I tried to keep it very short. Please do not hesitate to comment and tell me what you think or what I might have missed.
📚 As always, the script to the video with all the code examples lives here: github.com/cpp...
Links
CppInsights example mentioned at 06:14 - cppinsights.io...
Class Template Argument Deduction (CTAD) link: en.cppreferenc...
Google C++ Code Style on CTAD: google.github....
Howard Hinnant on vector of booleans: isocpp.org/blo...
Herb Sutter on vector of booleans: www.gotw.ca/pub...
Type traits: en.cppreferenc...
is_integral: en.cppreferenc...
Partial template specialization: en.cppreferenc...
Join this channel to get access to perks
/ @codeforyourself

Пікірлер: 13
@pitri_hub
@pitri_hub 9 ай бұрын
Template meta programming with (partial) specialization is such a wildly different language than to "regular" C++. And it's such an incredibly deep rabbit hole. Interestingly enough, my knowledge about this topic really helped to understand Haskell a lot better during the semester it has been taught in. The signature pattern matching in partial specializations is almost identical to how Haskell functions are overloaded for their use cases. C++ really helps to become a better programmer, because it exposes the user to a lot more concepts than certain other languages that deem certain mechanisms to low-level or dangerous to be exposed to the programmer. But seeing how C++ was helpful for such an unrelated language with very different paradigms was just wild. :D
@CodeForYourself
@CodeForYourself 9 ай бұрын
Thanks for sharing! Did not think of this parallel! I have only played a little with functional language but yes, the recursiveness of template meta programming I guess is what makes the experiences similar.
@harleyspeedthrust4013
@harleyspeedthrust4013 8 ай бұрын
I came back to c++ after spending some time with ocaml, rust, and idris, and everything was so much easier. This template stuff makes a lot more sense now and these videos are really good
@CodeForYourself
@CodeForYourself 8 ай бұрын
@harleyspeedthrust4013 thank you so much! Hope you keep enjoying the videos coming next! 🙌
@ChimiChanga1337
@ChimiChanga1337 9 ай бұрын
BRUH. The universe might actually be alive. I have been struggling to understand templates and was a bit sad that you didn't drop a video on them. Now here you are. FTW.
@CodeForYourself
@CodeForYourself 9 ай бұрын
Hope it helps 😅
@Maximus98245
@Maximus98245 7 ай бұрын
Great video! One thing that bothered me a bit was usage of " to specify template parameter. The parameter has nothing to do with class, it could be just as easily called , the word "class" gives a wrong impression, at least to me. Thanks!
@CodeForYourself
@CodeForYourself 7 ай бұрын
Ok, I agree with you that ClassT doesn’t hit the mark. What I prefer doing generally (when I cannot use concepts) is to give the template parameters meaningful names. Considering that this function is very illustrative and doesn’t really have a proper purpose it is hard to pick such a template parameter name, meaning the one that actually makes sense. So here I picked the ClassT as a substitute but I agree that it is a bit stupid and if I would have thought longer about it I probably would have just used T here. Thanks for your comment!
@PixelThorn
@PixelThorn 6 ай бұрын
For the win, my man
@Topbeautifulwomens
@Topbeautifulwomens Ай бұрын
2 1 3 4. i think you mentioned in previous videos that overloading precedes templates.
@DimaLyganov
@DimaLyganov 9 ай бұрын
Nice T-short, man!
@CodeForYourself
@CodeForYourself 9 ай бұрын
Thanks, Dima 😉
Why use templates in modern C++
12:32
Code for yourself
Рет қаралды 2,4 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
Use static in classes (when needed) in modern C++
16:34
Code for yourself
Рет қаралды 1,8 М.
Primary Constructors in C# 12 Explained!
9:39
James Montemagno
Рет қаралды 24 М.
Stop using std::vector wrong
23:14
The Cherno
Рет қаралды 174 М.
How to use FUNCTION TEMPLATES - a comprehensive guide for modern C++
23:33
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 1 МЛН
Type Traits
13:37
CopperSpice
Рет қаралды 14 М.
Re-inventing move semantics in modern C++ in 13 minutes
13:20
Code for yourself
Рет қаралды 7 М.