Generics in C++ 4 - Template Functions Full and Partial Specialization | Modern Cpp Series Ep. 74

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 พ.ย. 2024
  • ►Full C++ Series Playlist: • The C++ Programming La...
    ►Find full courses on: courses.mshah.io/
    ►Join as Member to Support the channel: / @mikeshah
    ►Lesson Description: In this lesson I discuss template specialization with function templates. This technique can also be used for class templates, but the motivating example will show you why it may be important to sometimes write specific code for handling specific types.
    ►TH-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!

ความคิดเห็น • 41

  • @RIDER500ful
    @RIDER500ful 10 หลายเดือนก่อน +3

    I usually don't comment on any videos. But after watching a few videos of yours(preparing for interviews), I feel obligated to thank you for making such usefull and informative videos.
    Please keep it up and make more indepth videos like this generics series.

    • @MikeShah
      @MikeShah  10 หลายเดือนก่อน

      Cheers, thank you for the kind words

  • @AgrawalRajat
    @AgrawalRajat 20 วันที่ผ่านมา +1

    Thanks a lot Mike, after working on C++ for 9 years, now I am getting my Template doubts cleared.

    • @MikeShah
      @MikeShah  20 วันที่ผ่านมา

      @@AgrawalRajat cheers happy to hear that!

  • @rahulindian123
    @rahulindian123 3 หลายเดือนก่อน +1

    Very detailed and informative explanation. thanks Mike for making such useful content.

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน

      Cheers!

  • @7guitarlover
    @7guitarlover ปีที่แล้ว +1

    The way you take us into the intricacy and detail is just wonderful ! Thankyou so much Dr Mike. =)

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers! Thank you for the kind words.

  • @prashlovessamosa
    @prashlovessamosa 8 หลายเดือนก่อน +3

    Mr Mike you are a goat 🐐

    • @MikeShah
      @MikeShah  8 หลายเดือนก่อน +1

      Thank you for the kind words 🙏

  • @bernhardkneer5393
    @bernhardkneer5393 3 หลายเดือนก่อน +1

    Hi Mike, thanks for your excellent videos. I was just struggling with the fact that if you define and declare in external cpp/h files to create a lib that would not work if the template function is called from another cpp file (e.g. main.cpp). According to your explanation it's clear because the compiler would create the versions of the template function according to their use and while e.g. template.cpp is getting compiled the compiler doesn't know how the function is being used, right? But how would that work with e.g. vector or vector? There must be a trick to make that happen... ;-)

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน

      I'm not sure I 100% understand the question, is it about using a vector in an external library? At some point the compiler needs to be able to see the 'blueprint' (i.e. the template) which it could get from a header file, stl_vector.h for instance should have the template file.

    • @bernhardkneer5393
      @bernhardkneer5393 2 หลายเดือนก่อน +1

      @@MikeShah thanks, so if I want to use the template functions/classes in several modules (cpp files) I have to implement them in a header file and include them in each cpp file? Wouldn't this cause the linker to complain about multiple defined symbols? The vector in my question was just an example of a template that is available by the standard library.

  • @derdere7803
    @derdere7803 ปีที่แล้ว +2

    great explanation. the way you use different tools to further clarify the topic is really unique and very helpful. Thanks for the effort and keep it up.

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers, thank you for the kind words!

  • @ieduardoms
    @ieduardoms ปีที่แล้ว +1

    Thank you very much. I always learn something new watching your videos.

  • @NH-ij8dz
    @NH-ij8dz 5 หลายเดือนก่อน +1

    This doesn't work when the function definition is in a header file.

    • @MikeShah
      @MikeShah  5 หลายเดือนก่อน

      I think if you make the functions 'inline' in the header you can then have the definition in the header.

  • @poojabajaj.
    @poojabajaj. 2 หลายเดือนก่อน

    Hey Mike! It's really constructive for the learners like me.
    Could you please make a video on template template parameters as well? Please...

  • @rassen2928
    @rassen2928 2 ปีที่แล้ว +3

    Partial specialization is the specialization made for a specific type (float, double)? And full specialization is the specialization made for every data type, the user can enter?

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว +2

      Correct, full specialization is filling in every single template parameter, and partial is setting at least one but not all paramters.

  • @nabil41854
    @nabil41854 2 ปีที่แล้ว +1

    Great work Mike

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      Thank you for the kind words!

  • @default2043
    @default2043 ปีที่แล้ว +1

    Hello Mike, I wanted to compliment your excellent video and commend your hard work! However, I do have a question. It appears that we're required to create numerous specialized templates, and I'm starting to wonder if this approach contradicts the core idea of maintaining a single code base that functions seamlessly across various data types. Is there an alternative approach for handling edge cases like float, double, and other data types that doesn't entail the creation of hundreds, if not thousands, of separate template functions?

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      Cheers! Template specializations usually start to show up during performance optimization :) So you tend to do this for specific use cases.

  • @dhanushs1802
    @dhanushs1802 2 ปีที่แล้ว +2

    Very well explained as always. Thank you.

    • @MikeShah
      @MikeShah  2 ปีที่แล้ว

      Thank you Dhanush!

  • @abelashenafi6291
    @abelashenafi6291 ปีที่แล้ว +1

    What is the difference between partial and full template specializaiton? Can you please make a video on that? Thanks. Your content is cool.

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      partial is simply providing some of the template arguments (and allowing any following to be user specified as normal), versus full which is providing them all!

  • @kafuu1
    @kafuu1 ปีที่แล้ว +1

    Nice Video!

  • @thestarinthesky_
    @thestarinthesky_ ปีที่แล้ว +1

    Can you please provide a video on perfect forwarding too? Thanks.

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      It's on the todo list 🙂

  • @relytheone853
    @relytheone853 ปีที่แล้ว

    Specialized is when you have the generic only, and when to write more specific types like ints and floats, is this partial? Please reply.

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      partial is when you specify a few of the template parameters but not all. Full specialization is when you specialize all of the template parameters.

    • @relytheone853
      @relytheone853 ปีที่แล้ว +1

      @@MikeShah Thank you, now it's clear to me. BTW, I'm reading C++ for game programmers...
      Thank you for your reply.

    • @MikeShah
      @MikeShah  ปีที่แล้ว +1

      @@relytheone853 awesome!

  • @ghulam2545
    @ghulam2545 2 ปีที่แล้ว +1

    interesting.

  • @MyMjrox
    @MyMjrox ปีที่แล้ว +1

    where is partial specialization?

    • @MikeShah
      @MikeShah  ปีที่แล้ว

      You're right, I should have had a second template example where I only make use of one of the template parameters. en.cppreference.com/w/cpp/language/partial_specialization