lundi 18 janvier 2016

How do you call a templated function by type name?

Suppose I've written the function

template <typename T> foo() { /* ... */ }

and now I want to do something like:

std::string s;
std::getline(std::cin, s);
if (!s.empty()) {
    invoke_foo_by_type_name(s);
}

that is, I want to get a type name from the input, dynamically - nothing I can know at compile time - and call the apporpriate version of foo. Question is, how do I do that? (Hopefully, without pre-registering all instances of foo in some factory myself) ... or maybe is there isn't at all a reasonable way to do this?





Aucun commentaire:

Enregistrer un commentaire