IfThenElse template

D

desktop

I am trying to understand the use of this template and how to use it:


// Primary template.
template<bool C, typename Ta, typename Tb>
class IfThenElse;

// Partial specialization, true.
template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb> {
public:
typedef Ta ResultT;
};

// Partial specialization, false.
template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb> {
public:
typedef Tb ResultT;
};

int main() {
IfThenElse<true,std::string,int> iff;
std::string str = iff.ResultT;
return 0;
}

When instantiated with true it should typedef the second argument
(std::string) to the public fieldResultT. But in main() I get an error
when I try to check the type of ResultT. Why is it not possible to
access ResultT?
 
M

Markus Schoder

I am trying to understand the use of this template and how to use it:


// Primary template.
template<bool C, typename Ta, typename Tb> class IfThenElse;

// Partial specialization, true.
template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb> {
public:
typedef Ta ResultT;
};

// Partial specialization, false.
template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb> {
public:
typedef Tb ResultT;
};

int main() {
IfThenElse<true,std::string,int> iff; std::string str = iff.ResultT;
return 0;
}

When instantiated with true it should typedef the second argument
(std::string) to the public fieldResultT. But in main() I get an error
when I try to check the type of ResultT. Why is it not possible to
access ResultT?

ResultT is a type so you must use it as a type:

int main() {
IfThenElse<true,std::string,int>::ResultT iff;
std::string str = iff;
return 0;
}
 
D

desktop

Markus said:
ResultT is a type so you must use it as a type:

int main() {
IfThenElse<true,std::string,int>::ResultT iff;
std::string str = iff;
return 0;
}


Ok but why does:

IfThenElse<true,std::string,int> bbb;
bbb.ResultT;

or:

IfThenElse<true,std::string,int> bbb;
bbb::ResultT;

work, it seems a bit more intuitive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top