Possible Duplicate:
Program to implement the is_same_type type trait in c++
I want my template function to do something differently based on whether the two typenames are equal or not:
template <typename T1, typename T2> f()
{
if (T1==T2) ...;
else ...;
}
I know "if(T1==T2)" is not gonna working, but, is there a way to do it?
You can compare the
typeid
ofT1
andT2
You can check the
boost::is_same
orstd::is_same
in C++11.So, it would be something like this:
Specialize the template
std::is_same
returns atypedef
of a boolean (true, false) depending on the equlity of the typesA
andB
If the types can be inferred and are not being explicitly passed, you could make two separate functions: