Separate declaration and definition for C++ templates? Yes please (sometimes)

  Рет қаралды 2,009

Code for yourself

Code for yourself

Күн бұрын

Пікірлер: 20
@CodeForYourself
@CodeForYourself 4 ай бұрын
Thanks a lot for watching, folks! Don't forget to share any thoughts you might have on this (or anything else)! 🙏
@BISONBOT94
@BISONBOT94 4 ай бұрын
I am new to C++ templates and ran into this exact issue about an hour ago. This video helped me a lot, thanks!
@CodeForYourself
@CodeForYourself 4 ай бұрын
@@BISONBOT94 awesome! Glad I could help out! 🙏
@alexanderbolinsky6430
@alexanderbolinsky6430 3 ай бұрын
Fantastic explanations and visuals!
@CodeForYourself
@CodeForYourself 3 ай бұрын
Thanks a lot for the compliments! 😌
@xavierfrazao1469
@xavierfrazao1469 4 ай бұрын
Great explanation. Thank you
@CodeForYourself
@CodeForYourself 4 ай бұрын
Glad you liked it! 🙏
@SystemSigma_
@SystemSigma_ 4 ай бұрын
If I'm not mistaken, this only helps speeding up compilation times due to the explicit template specialisation. Still, what other benefits do I get by splitting template declarations and definitions?
@CodeForYourself
@CodeForYourself 4 ай бұрын
I guess you're right to a degree but there is more to it. Explicit template instantiation (different from specialization) does indeed help the compile times. It helps because without it, the code would live in header files and so would be copied to any translation unit that includes that header, leading to the compiler needing to compile all of those instances on their own. If we put the code into a source file (and for that we need an explicit template instantiation) we will only compile the code once and link it to the places where it is used instead. Does this make any sense? Now as to other benefits, one big downside of header-only libraries, apart from compilation times, is that all the implementation actually lives within the header files. Meaning that if we want to distribute our library to other people we essentially have to distribute source code. So in the end, we have a sort of a tradeoff, header-only libraries are quite simple to use but might lead to long compile times as well as to needing to show our source code to anybody using our library. I talk about this at length in the video about libraries here: kzbin.info/www/bejne/gqnSaZmqnNGqjqcsi=JYNvd_2i6GLjfdvv
@SystemSigma_
@SystemSigma_ 4 ай бұрын
I'm perfectly aware of the standard benefits of splitting declaration and definition.. Still, for templates I really don't think it's worth the typing effort (if you're outside a smart ide) 😅 I guess the most valid point is only hiding the source code for distribution 😊
@CodeForYourself
@CodeForYourself 4 ай бұрын
@SystemSigma_ sometimes the compile times are important too. Some years ago I added a couple of explicit instantiations in a code base to save about an hour compile time 😅 I think if we *know* the types we’re about to use, then I would go for a compiled library as opposed to a header only one. It works also as an additional check for my design to a degree 🤷‍♂️
@SystemSigma_
@SystemSigma_ 4 ай бұрын
@CodeForYourself yeah, I used it with quite a success in a few embedded projects, but, of course, every project is different so 🥲
@CodeForYourself
@CodeForYourself 4 ай бұрын
@@SystemSigma_ yeah, I agree. 🙂‍↕️
@isfandyar3937
@isfandyar3937 4 ай бұрын
what would you do with template class
@CodeForYourself
@CodeForYourself 4 ай бұрын
There is an example of this towards the end of the video. Does that answer your question? Or should I give a better example? How can I make it better?
@CodeForYourself
@CodeForYourself 4 ай бұрын
@@isfandyar3937 also, I cover this extensively in this video: Why use templates in modern C++ kzbin.info/www/bejne/Z37VpWSEgph-pKs
@UvUtkarsh
@UvUtkarsh 4 ай бұрын
I wonder how the templates are used in data structures in STL (for ex: vector) so that it not only works with primitive data types like int, float,etc. (for which can do the template specialization) but also with user defined classes/structs, which is not known before hand. Any ideas ? @CodeForYourself
@CodeForYourself
@CodeForYourself 4 ай бұрын
@@UvUtkarsh generally the code is written in such a way that as long as the provided types follow some form of an interface they should work. They also use traits and partially specialize classes based on what those traits tell about the provided type. I might do an in-depth look into this later if there is interest
Why use templates in modern C++
12:32
Code for yourself
Рет қаралды 2,1 М.
amazing#devil #lilith #funny #shorts
00:15
Devil Lilith
Рет қаралды 18 МЛН
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 317 МЛН
Const correctness in C++
10:02
Code for yourself
Рет қаралды 3,3 М.
why do header files even exist?
10:53
Low Level
Рет қаралды 427 М.
The C++ Lambdas
11:44
Code for yourself
Рет қаралды 4,2 М.
Tinkering With Spirographs in C++
22:01
javidx9
Рет қаралды 21 М.
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 107 М.
amazing#devil #lilith #funny #shorts
00:15
Devil Lilith
Рет қаралды 18 МЛН