c++ - Template meta programming : print type list -


this question has answer here:

how can print type list ??

here tried + type list :

template<typename ...t> struct typelist; template<typename h, typename ...t> struct typelist<h, t...> {     using head = h;     using tail = typelist<t...>; public:     static inline void print(){         std::cout << h::name << " "; // how can name of h ???         typelist<t...>::print();     } };  template<> struct typelist<>{    static inline void print(){        std::cout << endl;    } }; 

so typelist<a,b,c,int>::print() give a b c int ,

a b , c user defined structs

can without adding static function called name in every struct?? there no compile time function returns type name ?

edit :

this not duplicate of this, did mention variables ??

you can use typeid(h).name() this. unfortunately, format of name() implementation defined, can't rely on getting output expect in cases.

for example, in gcc 4.9.2, typelist<a,b,c,int>::print() prints:

1a 1b 1c 

a possible solution define gettypename template function specialize different types, fall on typeid().name() when don't have specialization:

template <typename t> std::string gettypename() {     return typeid(t).name(); }  template <> std::string gettypename<int>() {     return "int"; }  template <> std::string gettypename<a>() {     return "a"; } 

this print out:

a 1b 1c int 

then add specializations other types.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -