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

  Рет қаралды 779

Code for yourself

Code for yourself

2 ай бұрын

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-for-yourself/s...
Links
- CppInsights example mentioned at 06:14 - cppinsights.io/s/6729874b
- Class Template Argument Deduction (CTAD) link: en.cppreference.com/w/cpp/lan...
- Google C++ Code Style on CTAD: google.github.io/styleguide/c...
- Howard Hinnant on vector of booleans: isocpp.org/blog/2012/11/on-ve...
- Herb Sutter on vector of booleans: www.gotw.ca/publications/mill0...
- Type traits: en.cppreference.com/w/cpp/hea...
- is_integral: en.cppreference.com/w/cpp/typ...
- Partial template specialization: en.cppreference.com/w/cpp/lan...
Join this channel to get access to perks
/ @codeforyourself

Пікірлер: 15
@pitri_hub
@pitri_hub 2 ай бұрын
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 2 ай бұрын
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 26 күн бұрын
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 26 күн бұрын
@harleyspeedthrust4013 thank you so much! Hope you keep enjoying the videos coming next! 🙌
@ChimiChanga1337
@ChimiChanga1337 2 ай бұрын
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 2 ай бұрын
Hope it helps 😅
@Maximus98245
@Maximus98245 21 күн бұрын
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 21 күн бұрын
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!
@DimaLyganov
@DimaLyganov 2 ай бұрын
Nice T-short, man!
@CodeForYourself
@CodeForYourself 2 ай бұрын
Thanks, Dima 😉
@gurumahendrakar65
@gurumahendrakar65 2 ай бұрын
Sir. You Can Make A Video C++ Virtual Tabes Dynamic Linking How Works. With assembly.
@CodeForYourself
@CodeForYourself 2 ай бұрын
Not sure I want to include assembly in that video but otherwise the vtable video is planned already! 😉
@gurumahendrakar65
@gurumahendrakar65 2 ай бұрын
@@CodeForYourself Just Waiting 🙋
@CodeForYourself
@CodeForYourself 2 ай бұрын
Will try my best to make it as soon as I can 👌
Do you even test? (your code with CMake)
12:38
Code for yourself
Рет қаралды 19 М.
Children deceived dad #comedy
00:19
yuzvikii_family
Рет қаралды 8 МЛН
🌊Насколько Глубокий Океан ? #shorts
00:42
你们会选择哪一辆呢#short #angel #clown
00:20
Super Beauty team
Рет қаралды 15 МЛН
I Can't Believe We Did This...
00:38
Stokes Twins
Рет қаралды 79 МЛН
TEMPLATES in Modern C++
9:31
cazz
Рет қаралды 26 М.
Re-inventing move semantics in modern C++ in 13 minutes
13:20
Code for yourself
Рет қаралды 6 М.
Clippy and Friends - Part 8 of Idiomatic Rust in Simple Steps
10:52
Templates under the hood
6:57
Code for yourself
Рет қаралды 1,1 М.
Const correctness in C++
10:02
Code for yourself
Рет қаралды 2,7 М.
ARRAYLIST VS LINKEDLIST
21:20
Core Dumped
Рет қаралды 53 М.
"I Hate Agile!" | Allen Holub On Why He Thinks Agile And Scrum Are Broken
8:33
CONCURRENCY IS NOT WHAT YOU THINK
16:59
Core Dumped
Рет қаралды 89 М.
8 TypeScript Tips To Expand Your Mind (and improve your code)
10:54
Children deceived dad #comedy
00:19
yuzvikii_family
Рет қаралды 8 МЛН