template <bool, class t, class u>// why is bool here,class booltype=bool. Are they equivalent?
struct if_
{
typedef typename t type;
};
template<class t, class u>
struct if_<false, t, u> // what does the <false,t,u> mean?
{
typedef typename u type;
};
The code if from an article named “name the template parameter “. I can’t understand the definition of the both struct.
EDIT: Moved the most important part first:
This conveniently defines a type that can conditionally be defined at compile-time.
Old Answer:
This means the first parameter passed to the template is a
bool.It’s not equivalent to
class booltype=bool. That would be a defaulttypenamefor the template.This is a specialization of your
struct. If the first parameter passed to the template is false, this it what thestructis defined as.Basically, assume:
and
The notation basically tells what to typedef – if the first parameter is
true,typedefthe second parameter, otherwise the third.