liximomo
liximomo

Reputation: 388

"Name The Template Parameter" Odd Definition

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.

Upvotes: 4

Views: 86

Answers (3)

Paul Michalik
Paul Michalik

Reputation: 4381

struct if_<false, t, u> is a partial sspecialization of the primary template if_ which is instantiated by the compiler whenever the first parameter of the template has the value false, regardless of the other two "type-parameters". You're dealing with "non-type" template parameters here. The primary purpose of this construction is to select either the type t or type u, depending on the value of the first parameter, so that

if_<true, int, double>::type

will be of type int

and

if_<false, int, double>::type

will be of type double. Admittedly quite difficult to grasp if it's your "first contact" with c++ meta-programming, but basically you get an equivalent of the traditional if statement evaluated by the compiler at compile-time.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

EDIT: Moved the most important part first:

if_<true, int, double>::type;  // this is int
if_<false, int, double>::type; // this is double

This conveniently defines a type that can conditionally be defined at compile-time.

Old Answer:

template <bool, class t, class u>// why is bool here,class booltype=bool. Are they equivalent?
struct if_
{
    typedef typename t type;
};

This means the first parameter passed to the template is a bool.

It's not equivalent to class booltype=bool. That would be a default typename for the template.

template<class t, class u>
struct if_<false,  t,  u>   // what does the <false,t,u> mean?   
{
    typedef typename u type;
};

This is a specialization of your struct. If the first parameter passed to the template is false, this it what the struct is defined as.

Basically, assume:

if_<true, int, double> x;
//is defined as
//struct if_
//{
//    typedef int type;
//};

and

if_<false, int, double> x;
//is defined as
//struct if_
//{
//    typedef double type;
//};

The notation basically tells what to typedef - if the first parameter is true, typedef the second parameter, otherwise the third.

Upvotes: 4

Attila
Attila

Reputation: 28762

The first is a general template definition of type if_. The bool is a template parameter (template parameters can be types or integral values)

The second is a partial specialization of the same template. PArtial specialization means that you set some of the template parameters, but not all.

Which one gets used is decided like this: if there is an excplicit specialization for the actual template parameters, that specialization is used, otherwise the non-specialized (the forst definition in this case). The non-specialized version acts a "default" selection

Upvotes: 0

Related Questions