Hello and welcome to C++ Weekly. I'm your host, Jason Turner, available for code reviews and
on-site training. Now in this episode I'm going to directly address this question that
came from a viewer, so be sure that you check out the list of episode ideas so that you can
submit your own request and we can see if they get thumbs up and become an episode in the future.
[Music]
Okay, so the viewer just asks, "Do we have template 'typename' vs 'class'?" I will give
you my arguments for one or the other, and I will show you what I think makes sense,
and I'll show you one case where you don't get a choice. So before we continue with this,
again, be sure that you check out the video description for links to the code
samples and updated information about the channel and swag and all the things that
are available - t-shirts, ways to support the channel. Yeah, shameless self-promotion here,
but also make sure that you hire me to come out to your company to do on-site
training because we can talk about all these things in person if you're interested in that.
Okay so the viewer was asking template 'typename' vs template 'class', and it comes down to this.
I'm going to write some sort of function and it takes two unknown types, and I'm going to
go ahead and fill those in here, and I'm going to point out that I can do 'typename' or 'class'. In
this particular context they mean the exact same thing. Now my understanding is that it was C++
(pre-C++98) only allowed 'class' here, and then once C++98 was standardized
they allowed 'typename'. So that's been a long time - that's 25 years ago from the recording
of this episode of C++ Weekly. My personal thought is that if I'm reading this code,
I expect that the user must provide a 'class' type. Now 'class' types are unions, class, struct.
Those are the three that are 'class' types, and so that would include something like std::string,
user-defined types or whatever; but ints and floats are not 'class' types.
This code is using function template argument deduction, and I can, in fact, pass an int
and a float to this thing; and of course it does nothing. If I change this to 'typename',
then the behavior of this code changes not at all. So to get my specific argument,
and this is completely arbitrary, it's just my personal way of reading this code,
I think 'typename' makes more sense here, but to clarify it does not make any difference.
Now there is one case where you are forced to use 'class' instead of 'typename'. This
example is for an upcoming episode that I have not recorded yet, and it is the example of a template
template. So let's be clear,
this is a template that requires a template parameter be passed to it, and notice for
the innermost template I'm allowed to use 'typename', but for the template template itself
I must use 'class' here. That is the only place that I must use 'class' where otherwise
'typename' would be allowed in a template context; and I guess it makes sense because
a template template is going to be a 'class' type, so that seems pretty fair; but yeah,
that is the only case where you must use 'class', and I always prefer 'typename'. So hopefully
this was an instructional video. Not terribly confusing, but it is truly a matter of preference,
but since the viewer asked for my opinion, that is my opinion. So I hope that you learned something
from this episode of C++ Weekly. Be sure to subscribe, leave a thumbs up, all the things...
[Music]