c++ - How to pass multi-argument templates to macros? -
say have macro this:
#define set_type_name(type, name) \ template<typename t> \ std::string name(); \ \ template<> \ std::string name<type>() { \ return name; \ }
this won't work if pass template has more 1 parameter, because comma in <int, int>
interpreted separating macro arguments, not template arguments.
set_type_name(std::map<int, int>, "themap") // error: macro expects 2 arguments, 3 given
this problem seems solved doing this:
set_type_name((std::map<int, int>), "themap")
but problem arises, 1 did not expect:
template<> std::string name<(std::map<int, int>)>() // template argument 1 invalid
it seems parentheses make template argument invalid. there way around this?
besides typedef
, switch order of arguments , use variadic macros (requires c99 or c++11-compatible compiler):
#define set_type_name(name, ...) \ template<typename t> \ std::string name(); \ \ template<> \ std::string name<__va_args__>() { \ return name; \ }
...
set_type_name("themap", std::map<int, int>)
Comments
Post a Comment