Can I obtain C++ type names in a constexpr way?
问题 I would like to use the name of a type at compile time. For example, suppose I've written: constexpr size_t my_strlen(const char* s) { const char* cp = s; while(*cp != '\0') { cp++; }; return cp - s; } and now I want to have: template <typename T> constexpr auto type_name_length = my_strlen(typeid(T).name()); But alas, typeid(T).name() is just const char* , not constexpr... is there some other, constexpr way to get a type's name? 回答1: Well, you could, sort of, but probably not quite portable: