Sunday 15 May 2011

c++11 - C++ type trait to extract specialization value of template argument -



c++11 - C++ type trait to extract specialization value of template argument -

i have next template:

template<typename t, const char *name_ > struct named { typedef t type; static constexpr const char *name = name_; };

i'd have type traits which:

would extract type , name (2 different) if argument type "named" would extract original type , empty string if argument different type.

example:

template<typename t> void foo() { typename cppdi::extract_type<t>::type x; std::cout << "type: " << typeid(x).name() << ", name: " << cppdi::extract_name<t>::value << std::endl; } char bar[] = "bar"; void test() { foo<int>(); // type: i, name: foo<named<int, bar>>(); // type: i, name: bar }

is possible implement such extract_type , extract_name?

write traits this:

template< typename t > struct extract_type { using type = t; }; template< typename t, const char* s > struct extract_type< named< t, s > > { using type = t; }; template< typename t > struct extract_name { static constexpr const char* value = ""; }; template< typename t, const char* s > struct extract_name< named< t, s > > { static constexpr const char* value = s; };

that lone won't work, code gave illegal in several places. fixed them in live example.

c++ c++11 typetraits

No comments:

Post a Comment