Say I have a function with template type T and two other classes A and B.
template <typename T>
void func(const T & t)
{
...........
//check if T == A do something
...........
//check if T == B do some other thing
}
How can I do these two checks (without using Boost library)?
If you literally just want a boolean to test whether
T == A, then you can useis_same, available in C++11 asstd::is_same, or prior to that in TR1 asstd::tr1::is_same:You can trivially write this little class yourself:
Often though you may find it more convenient to pack your branching code into separate classes or functions which you specialize for
AandB, as that will give you a compile-time conditional. By contrast, checkingif (T_is_A)can only be done at runtime.