简体   繁体   中英

Specify the type of a vector with a string

How do you specify a type with a string? I mean:

string s = "int";
Vector<s> vec;

And I want vec to be vector<int> . Is this possible?

I want to make a class where the user can type in a string and a vector with that type will be created.

Not possible in C++, atleast not the way you want.

Templates are a compile time concept, while user input is a runtime concept. Completely different, not mixable.

To make that work, you need a dynamically typed language, which C++ is not. It's statically typed.

Is this possible?

This is not possible in C++. If using boost is an option, consider creating a vector of boost::variant objects instead: this way, your statically-typed vector would be prepared to accept elements of different types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM