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?
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: ua-cam.com/video/Lxo8ftglwXE/v-deo.htmlsi=JYNvd_2i6GLjfdvv
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 😊
@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 🤷♂️
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
@@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
Thanks a lot for watching, folks! Don't forget to share any thoughts you might have on this (or anything else)! 🙏
I am new to C++ templates and ran into this exact issue about an hour ago. This video helped me a lot, thanks!
@@BISONBOT94 awesome! Glad I could help out! 🙏
Great explanation. Thank you
Glad you liked it! 🙏
Fantastic explanations and visuals!
Thanks a lot for the compliments! 😌
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?
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: ua-cam.com/video/Lxo8ftglwXE/v-deo.htmlsi=JYNvd_2i6GLjfdvv
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 😊
@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 🤷♂️
@CodeForYourself yeah, I used it with quite a success in a few embedded projects, but, of course, every project is different so 🥲
@@SystemSigma_ yeah, I agree. 🙂↕️
what would you do with template class
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?
@@isfandyar3937 also, I cover this extensively in this video: Why use templates in modern C++
ua-cam.com/video/1Mrt1NM3KnI/v-deo.html
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
@@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