I use a lot of templates, and sometimes itโs hard for me to figure out what type it really is. I wanted to write a utility to give me a pretty, pretty string name for each type - typeid () just doesn't cut. For example, if I have only vector<int> , gcc.4.6.4 in my field creates the following with a type:
St6vectorIiSaIiEE
ideally want
std::vector< int, std::allocator< int > >
I wrote something that will work with any type or pattern by type, but just provide two patterns:
template <typename T> struct simple_type_name; template <template <typename....> class T> struct template_type_name;
Which, when specializing in int or std::vector , can help me create the strings I want. I also have a partial specialization simple_type_name only on any Base<Args...> , to go through all the arguments and do everything as needed. This is great for int and vector<int> and indeed any arbitrarily complex template material ... as long as all templates are types.
If this helps, my version of the โfull templateโ looks like this:
template <template <typename...> class Base, typename... Args> struct simple_type_name<Base<Args...>> { static std::string name(int indent = 0) { std::string base = template_type_name<Base>::name(indent); std::string args[] = { simple_type_name<Args>::name(indent + 4)... } ;
Question : how can I do what I work for, say, std::array<int, 10> ? I do not know how to handle parameters other than type. Is it possible?
c ++ c ++ 11 templates
Barry
source share