c++ typename
Have you ever tried to dynamically get the typename (like “int” or “char *”) of a variable? in g++ you will get some curios effects
| type | typeid(type).name() |
|---|---|
int |
i |
std::string |
Ss |
char * |
Pc |
std::vector<std::string> |
St6vectorISsSaISsEE |
These names are unique but they are not “human-readable”.
I wrote a small header file defining a template function resolving exactly this problem.
after including TypeName.h you could write instead of
int i=5;
cout << typeid(i).name();
int i=5;
cout << typeName(i);
which is
- shorter and
- smarter 😉
It is a restriction of c++ that only commonly used types are checked (It is not possible to make it generic types). These could be easily extended.
Every one of the following types is currently checked with any combination of “(|const) type (|*|**|*&)” (RegExpr-like-syntax):
std::stringcharshortintlongunsigned charunsigned shortunsigned intunsigned longfloatdoublelong doublevoid