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 check the boost::is_same
or std::is_same
in C++11.
So, it would be something like this:
template <typename T1, typename T2> f()
{
if (boost::is_same<T1,T2>::value) ...;
else ...;
}
#include <type_traits>
template <typename A, typename B> void f() {
if ( std::is_same<A, B>::value ) {
//
}
}
std::is_same
returns a typedef
of a boolean (true, false) depending on the equlity of the types A
and B
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With