C++ Templates - Part 2: Basic syntax, specialization, tag dispatch and SFINAE

preview_player
Показать описание
YouTube has removed the ability to add annotations, so I'm putting this in the description. I said I wasn't going to talk about dependent types (which I should actually call dependent names), but there's no reason not to. Some of the contents of a template may vary in meaning depending on the template type, so you have to specify when a name inside a template is referring to a type.
Рекомендации по теме
Комментарии
Автор

Excellent video. I liked how you walk through the evaluation process. And all with a smile.

enriquebenedicto
Автор

The Best template tutorial!!
What a pity Google's algorithm can't find out!
You should do more.

thisismuffinmuffin
Автор

The best thing is not only to demonstrate a perfect flawless interactive presentation but make some errors, show them, solve them and explain what went wrong. When you do this the first time you get 90% errors before hitting the sweet spot that compiles.

GreatTutorialChannel
Автор

With all this stuff still a smiling C++ Programmer!!! Respect👍👍👍

tomaspyth
Автор

Loved your videos about templates and how you enjoy explaining them with passion! I hope you'll make some more of these videos in the future!
You're truly a great teacher.
One thing I would like to suggest though, if you would make a separate channel just for programming videos, that would attract much more subscribers, as some of the viewers are not interested in other videos rather than learning about programming. You could also open Patreon account and add to your videos description for people who would like to support what you do here, as the content and the examples you provided are very valuable and helpful.
Big like and thanks!

giladreich
Автор

Very good guidance!

To expand a little more information and clarify the examples, forms 2 and 3 of SFINAE are overloads instead of template specializations.

In case both full and partial specializations are needed for a function, MIXING both specializations and overloads (overloads to workaround function partial specialization not being allowed) won't work when the template function is used by means of forward declaration while all its definitions (specialized and overloaded) are in a different translation unit. (Unless all overloads are also forward declared, which defeats de purpose of having the single template function forward declared in the translation unit using it).

True for any mixing of specializations and overloads including those overload forms of SFINAE. Reason being, the compiler will only try to find and match one of the specializations, oblivious to any overloads.

Pre C++20 concepts, I've managed to overcome this only with tag dispatching.

tubemelf
Автор

For function overloads, you shouldn't create specializations, but instead just create the overload as if it were like fmax:


template<typename T> bool Equals( T lhs, T rhs ){ return lhs == rhs; }
bool Equals( float lhs, float rhs ) {return true; }


template specializations aren't suppose to participate in overload resolution.


Just for passers by, instead of typing std::is_floating_point<T>::value, you can also use the shortcut std::is_floating_point_v<T>. For types, the shortcuts or aliases or typedefs end in _t and for values, the shortcuts end in _v.


std::is_integral_v<T>
std::is_floating_point_v<T>
std::conditional_t<true, int, float>
std::enable_if_t<false, int>


I'm so glad C++17 added constexpr-if statements, now you can avoid tag dispatch and sfinae,


template<typename T> bool Equals( T lhs, T rhs )
{
if constexpr( std::is_floating_point_v<T> )

{
return std::abs( rhs - lhs ) < static_cast<T>(0.00001);
}
else
{
return lhs == rhs;
}
}

MrAlbinopapa
Автор

Echo that below. The best c++ vids on YouTube. Great way of teaching. Effortless... Seemingly.

jonathanmoore
Автор

May I know where and how you learned all these? amazing!

xudongsun
Автор

Did I see The Cherno walking around in the background?

readingchess
Автор

"People usually don't go out of their way to make void pointers" <- such a fucking funny statement. :P

antiHUMANDesigns
Автор

Just like [*<>::type] can be simplified with [*_t<>], [*<>::value] can also be simplified with [*_v<>] in C++17.

youcefsb
Автор

Can we define template functions depending of a the value of the argument not its type?

mohammedsamir
Автор

Great tutorials, thx!

One comment: it seems strange that the compiler would need help confirming that `conditional<is_floating_point<T>::value, true_type, false_type>::type` is indeed a type. It seems evident from the syntax: if it's not a type, the `{}` right after it would be an error wouldn't it? And so couldn't the compiler simply assume it's a type from the syntax (and complain if it later discovers that it's actually not a type)? In fact, I tried to drop `typename` from `typename conditional<is_floating_point<T>::value, true_type, false_type>::type{}` and MSVC 2017 compiled it without an error.

Also, is there any reason to prefer the (newer) SFINAE over the (older) tag approach?

muckvix
Автор

27:19 I think will always be a void pointer, no matter what type of T is. Is my opinion right or wrong?

us
Автор

Really great tutorial. I liked your presentation style :)

kaustubhbansal
Автор

Nice explanation.I have doubt on iterator Why we need to provide specialization in iterator


template <typename Iterator>
void process(Iterator begin, Iterator end)
{
for (; itr != end; ++itr) {
process(*itr);
}
}


why i can't write


void process(Iterator begin, Iterator end)
{
for (; itr != end; ++itr) {
process(*itr);
}
}


please explain

TarunSingh-jemy
Автор

the first example of tag dispatch doesn't compile on my machine, I have VS2017 15.9 v14.16, any help ?

ahmadalastal
Автор

Very good stuff: good examples, good explanation. Subscribed!

illya_ike
Автор

Regarding what you showed at the end: "The behavior of a program that adds specializations for is_floating_point or is_floating_point_v (since C++17) is undefined."

sebastianwilke