Reputation: 8490
How can I instanciate a generic type in c++?
If I want to create a List<X>
where X
is object. How can I do that?
Upvotes: 3
Views: 540
Reputation: 310980
Just instantiate List. As Generics are erased by the compiler, this is fully equivalent to List at runtime.
Upvotes: 4
Reputation: 533680
Given you cannot instantiate a generic type at runtime in Java, I am pretty sure you can't do it in C++.
Generics are a compile time feature. Unless you want to pass what you produce to a compiler e.g. as plain Java code in text, generics are unlikely to be useful to you. (And if you did want to do this I suspect C++ wouldn't be your best choice)
If I want to create a List where X is object
In Java X
is a type of reference to an object. As the code from all Lists only hold references, the code is the same and is not templated like in C++. This means you can't use a primitives or structs or objects, only references.
Upvotes: 1