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

Поділитися
Вставка
  • Опубліковано 15 гру 2024

КОМЕНТАРІ • 20

  • @CodeForYourself
    @CodeForYourself  5 місяців тому +2

    Thanks a lot for watching, folks! Don't forget to share any thoughts you might have on this (or anything else)! 🙏

  • @BISONBOT94
    @BISONBOT94 5 місяців тому +1

    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! 🙏

  • @xavierfrazao1469
    @xavierfrazao1469 5 місяців тому +2

    Great explanation. Thank you

  • @alexanderbolinsky6430
    @alexanderbolinsky6430 4 місяці тому

    Fantastic explanations and visuals!

  • @SystemSigma_
    @SystemSigma_ 5 місяців тому +1

    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: ua-cam.com/video/Lxo8ftglwXE/v-deo.htmlsi=JYNvd_2i6GLjfdvv

    • @SystemSigma_
      @SystemSigma_ 5 місяців тому +2

      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 місяців тому +3

      @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 місяців тому +1

      @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. 🙂‍↕️

  • @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++
      ua-cam.com/video/1Mrt1NM3KnI/v-deo.html

  • @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 місяців тому +1

      @@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