How to use CLASS TEMPLATES, type traits, partial and full class template specialization

Поделиться
HTML-код
  • Опубликовано: 13 сен 2024
  • Class templates with the ability to partially and fully specialize them are arguably what makes C++ so powerful! We cover most of what one needs to know about what class templates are, how to specialize them fully or partially as well as how it all plays its role if we want to implement type traits.
    Before you watch this video I recommend you to watch these videos if you haven't already:
    - why use templates: • Why use templates in m...
    - what templates do under the hood: • Templates under the hood
    - how to use function templates: • How to use FUNCTION TE...
    In this video we look at
    - Class method templates
    - Class templates
    - How they can be specialized partially and fully
    - And as always some easy to follow examples as well as stories about what can go wrong with template specialization
    We cover a lot of ground in this video, and it is quite long as much as I tried to keep it very short. Please do not hesitate to comment and tell me what you think or what I might have missed.
    📚 As always, the script to the video with all the code examples lives here: github.com/cpp...
    Links
    - CppInsights example mentioned at 06:14 - cppinsights.io...
    - Class Template Argument Deduction (CTAD) link: en.cppreferenc...
    - Google C++ Code Style on CTAD: google.github....
    - Howard Hinnant on vector of booleans: isocpp.org/blo...
    - Herb Sutter on vector of booleans: www.gotw.ca/pub...
    - Type traits: en.cppreferenc...
    - is_integral: en.cppreferenc...
    - Partial template specialization: en.cppreferenc...
    Join this channel to get access to perks
    / @codeforyourself

Комментарии • 16

  • @pitri_hub
    @pitri_hub 4 месяца назад +3

    Template meta programming with (partial) specialization is such a wildly different language than to "regular" C++. And it's such an incredibly deep rabbit hole.
    Interestingly enough, my knowledge about this topic really helped to understand Haskell a lot better during the semester it has been taught in. The signature pattern matching in partial specializations is almost identical to how Haskell functions are overloaded for their use cases.
    C++ really helps to become a better programmer, because it exposes the user to a lot more concepts than certain other languages that deem certain mechanisms to low-level or dangerous to be exposed to the programmer. But seeing how C++ was helpful for such an unrelated language with very different paradigms was just wild. :D

    • @CodeForYourself
      @CodeForYourself  4 месяца назад +1

      Thanks for sharing! Did not think of this parallel!
      I have only played a little with functional language but yes, the recursiveness of template meta programming I guess is what makes the experiences similar.

    • @harleyspeedthrust4013
      @harleyspeedthrust4013 3 месяца назад +2

      I came back to c++ after spending some time with ocaml, rust, and idris, and everything was so much easier. This template stuff makes a lot more sense now and these videos are really good

    • @CodeForYourself
      @CodeForYourself  3 месяца назад +1

      @harleyspeedthrust4013 thank you so much! Hope you keep enjoying the videos coming next! 🙌

  • @ChimiChanga1337
    @ChimiChanga1337 4 месяца назад +2

    BRUH. The universe might actually be alive. I have been struggling to understand templates and was a bit sad that you didn't drop a video on them. Now here you are. FTW.

  • @Maximus98245
    @Maximus98245 2 месяца назад +1

    Great video! One thing that bothered me a bit was usage of " to specify template parameter. The parameter has nothing to do with class, it could be just as easily called , the word "class" gives a wrong impression, at least to me. Thanks!

    • @CodeForYourself
      @CodeForYourself  2 месяца назад

      Ok, I agree with you that ClassT doesn’t hit the mark. What I prefer doing generally (when I cannot use concepts) is to give the template parameters meaningful names. Considering that this function is very illustrative and doesn’t really have a proper purpose it is hard to pick such a template parameter name, meaning the one that actually makes sense. So here I picked the ClassT as a substitute but I agree that it is a bit stupid and if I would have thought longer about it I probably would have just used T here. Thanks for your comment!

    • @PixelThorn
      @PixelThorn Месяц назад

      For the win, my man

  • @DimaLyganov
    @DimaLyganov 4 месяца назад

    Nice T-short, man!

  • @gurumahendrakar65
    @gurumahendrakar65 4 месяца назад

    Sir. You Can Make A Video C++ Virtual Tabes Dynamic Linking How Works. With assembly.

    • @CodeForYourself
      @CodeForYourself  4 месяца назад +1

      Not sure I want to include assembly in that video but otherwise the vtable video is planned already! 😉

    • @gurumahendrakar65
      @gurumahendrakar65 4 месяца назад +2

      @@CodeForYourself Just Waiting 🙋

    • @CodeForYourself
      @CodeForYourself  4 месяца назад +1

      Will try my best to make it as soon as I can 👌