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

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

Code for yourself

Code for yourself

Күн бұрын

Пікірлер: 20
@CodeForYourself
@CodeForYourself 5 ай бұрын
Thanks a lot for watching, folks! Don't forget to share any thoughts you might have on this (or anything else)! 🙏
@BISONBOT94
@BISONBOT94 5 ай бұрын
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 5 ай бұрын
@@BISONBOT94 awesome! Glad I could help out! 🙏
@alexanderbolinsky6430
@alexanderbolinsky6430 4 ай бұрын
Fantastic explanations and visuals!
@CodeForYourself
@CodeForYourself 4 ай бұрын
Thanks a lot for the compliments! 😌
@xavierfrazao1469
@xavierfrazao1469 5 ай бұрын
Great explanation. Thank you
@CodeForYourself
@CodeForYourself 5 ай бұрын
Glad you liked it! 🙏
@SystemSigma_
@SystemSigma_ 5 ай бұрын
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 5 ай бұрын
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_ 5 ай бұрын
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 5 ай бұрын
@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_ 5 ай бұрын
@CodeForYourself yeah, I used it with quite a success in a few embedded projects, but, of course, every project is different so 🥲
@CodeForYourself
@CodeForYourself 5 ай бұрын
@@SystemSigma_ yeah, I agree. 🙂‍↕️
@UvUtkarsh
@UvUtkarsh 5 ай бұрын
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 5 ай бұрын
@@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
@isfandyar3937
@isfandyar3937 5 ай бұрын
what would you do with template class
@CodeForYourself
@CodeForYourself 5 ай бұрын
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 5 ай бұрын
@@isfandyar3937 also, I cover this extensively in this video: Why use templates in modern C++ kzbin.info/www/bejne/Z37VpWSEgph-pKs
Why use templates in modern C++
12:32
Code for yourself
Рет қаралды 2,2 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Re-inventing move semantics in modern C++ in 13 minutes
13:20
Code for yourself
Рет қаралды 7 М.
Templates under the hood
6:57
Code for yourself
Рет қаралды 1,4 М.
The 3 Laws of Writing Readable Code
5:28
Kantan Coding
Рет қаралды 784 М.
Does AI Coding Make Programmers Faster
11:27
Ian Sinnott
Рет қаралды 357
Don't use static (outside of classes) in modern C++
15:56
Code for yourself
Рет қаралды 6 М.
The C++ Lambdas
11:44
Code for yourself
Рет қаралды 4,6 М.
Headers and libraries, but with classes
7:07
Code for yourself
Рет қаралды 1,7 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН