ad_ad
ad_ad

Reputation: 385

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?

Upvotes: 2

Views: 2727

Answers (1)

Christian Hackl
Christian Hackl

Reputation: 27518

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.

Upvotes: 8

Related Questions