c++c++11typestypeinfo

Get type_info from typename


Is there a way to get std::type_info from the type's name? For example,

std::type_info f(std::string name) {
std::type_info info; 
...
return info;
}

int main() {
const std::string name = typeid(double).name();
std::type_info info = f(name);
assert(info==typeid(double));
}

What would the function f be?


Solution

  • No. As documentation for std::type_info::name says:

    No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.

    I don't know your exact use case, but chances are you can utilise C++11 std::type_index instead.