|
-
May 27th, 2015, 06:42 AM
#14
Re: Vector of Templates Problem
 Originally Posted by Fateslayer
I think it's okay to use templates with main() function.
you can use templates in main()
but you cannot turn main into a templatized function. which is what you seem to be doing in your OP.
Just FYI since you don't seem to 'get' it.
much simplified (it's really more elaborate than this)... template parameters in C++ are program-time placeholders for the actual type that will be decided/substituted at compile time.
When you make a vector<T> then at compile time this results in a very specific vector-of-a-certain-type, and ALL ELEMENTS in the vector will be of THE SAME type (!!! yes, read this again 10 times so you understand this).
The template parameter(s) are in no shape or form an indication of "this type will be decided when the executable is running". As stated before, the type has been decide/fixed at compile time and cannot change to something else at runtime.
So if you want a vector of "at runtime decided data", you need to first create or use a class that can do just that. runtime decided data. and then you can make a vector of that type.
there's boost::any, boost::variant, and there are other types that could serve the same purpose such as the COM VARIANT type (or the MFC/ATL wrappers thereof: COleVariant) and you can create your own type
note that other languages have built in support for "any"/"variant" types because they derive all their types from a single 'object' type.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|