Thanks a lot for watching, folks! Don't forget to share any thoughts you might have on this (or anything else)! 🙏
@BISONBOT944 ай бұрын
I am new to C++ templates and ran into this exact issue about an hour ago. This video helped me a lot, thanks!
@CodeForYourself4 ай бұрын
@@BISONBOT94 awesome! Glad I could help out! 🙏
@alexanderbolinsky64303 ай бұрын
Fantastic explanations and visuals!
@CodeForYourself3 ай бұрын
Thanks a lot for the compliments! 😌
@xavierfrazao14694 ай бұрын
Great explanation. Thank you
@CodeForYourself4 ай бұрын
Glad you liked it! 🙏
@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?
@CodeForYourself4 ай бұрын
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_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 😊
@CodeForYourself4 ай бұрын
@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_4 ай бұрын
@CodeForYourself yeah, I used it with quite a success in a few embedded projects, but, of course, every project is different so 🥲
@CodeForYourself4 ай бұрын
@@SystemSigma_ yeah, I agree. 🙂↕️
@isfandyar39374 ай бұрын
what would you do with template class
@CodeForYourself4 ай бұрын
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?
@CodeForYourself4 ай бұрын
@@isfandyar3937 also, I cover this extensively in this video: Why use templates in modern C++ kzbin.info/www/bejne/Z37VpWSEgph-pKs
@UvUtkarsh4 ай бұрын
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
@CodeForYourself4 ай бұрын
@@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