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!
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.
Cheers, thank you for the kind words
Thanks a lot Mike, after working on C++ for 9 years, now I am getting my Template doubts cleared.
@@AgrawalRajat cheers happy to hear that!
Very detailed and informative explanation. thanks Mike for making such useful content.
Cheers!
The way you take us into the intricacy and detail is just wonderful ! Thankyou so much Dr Mike. =)
Cheers! Thank you for the kind words.
Mr Mike you are a goat 🐐
Thank you for the kind words 🙏
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... ;-)
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.
@@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.
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.
Cheers, thank you for the kind words!
Thank you very much. I always learn something new watching your videos.
Cheers!
This doesn't work when the function definition is in a header file.
I think if you make the functions 'inline' in the header you can then have the definition in the header.
Hey Mike! It's really constructive for the learners like me.
Could you please make a video on template template parameters as well? Please...
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?
Correct, full specialization is filling in every single template parameter, and partial is setting at least one but not all paramters.
Great work Mike
Thank you for the kind words!
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?
Cheers! Template specializations usually start to show up during performance optimization :) So you tend to do this for specific use cases.
Very well explained as always. Thank you.
Thank you Dhanush!
What is the difference between partial and full template specializaiton? Can you please make a video on that? Thanks. Your content is cool.
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!
Nice Video!
Cheers!
Can you please provide a video on perfect forwarding too? Thanks.
It's on the todo list 🙂
Specialized is when you have the generic only, and when to write more specific types like ints and floats, is this partial? Please reply.
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.
@@MikeShah Thank you, now it's clear to me. BTW, I'm reading C++ for game programmers...
Thank you for your reply.
@@relytheone853 awesome!
interesting.
where is partial specialization?
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