I'd like to specialize class for some type of classes, for example based on std::is_arithmetic. Although compiler doesn't "see" my specialization based on "enable_if" and chooses principle/main template. Could you please help me with this... Below is snippet of code and output after compiling with g++ 4.8
#include < iostream >
#include < type_traits >
#include < string >
template < typename T1, typename T2 = void >
struct TestT
{
static const bool is_int = false;
static const bool is_str = false;
};
template < typename T>
struct TestT < T,
std::enable_if< std::is_arithmetic<t>::value, T >::type >
{
static const bool is_int = false;
static const bool is_str = false;
};
template < typename T>
struct TestT < std::string, T >
{
static const bool is_int = false;
static const bool is_str = true;
};
class enum TestE
{
Last
};
int main(int argc, char* argv[])
{
std::cout << "Enum is_int: " << TestT<TestE>::is_int
<< ", is_str: " << TestT<TestE>::is_str << std::endl;
std::cout << "string is_int: " << TestT<std::string>::is_int
<< ", is_str: " << TestT<std::string>::is_str << std::endl;
std::cout << "int is_int: " << TestT<int>::is_int
<< ", is_str: " << TestT<int>::is_str << std::endl;
return 0;
}
Output of above is:
Enum is_int: 0, is_str: 0// Expected
string is_int: 0, is_str: 1// Expected
int is_int: 0, is_str: 0// Not expected
I really would appreciate for any help, and thank you in advance
Aucun commentaire:
Enregistrer un commentaire