5

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.

sth
  • 222,467
  • 53
  • 283
  • 367
shizzle
  • 183
  • 2
  • 12

2 Answers2

5

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.

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • Ok, but I want to do this because i want to implement a multidimensional vector, http://stackoverflow.com/questions/8579207/hypercube-c-multidimensional-vectros . Any tips? – shizzle Dec 20 '11 at 21:11
3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523