-
Vector of Templates Problem
I want to create a vector of templates but I'm getting 2 linker errors.
I am doing the following :
template<typename T>
int main()
{
vector<T> exp;
return 0;
}
I really need to store elements of different types in the same vector. Please help thank you.
-
Re: Vector of Templates Problem
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
VictorN
I don't know anything about boost so can u provide code on how to use it to make vector which can hold any data type using the code I mentioned in the post
-
Re: Vector of Templates Problem
Quote:
I really need to store elements of different types in the same vector.
Why? What are you trying to accomplish? c++ is a strongly typed language so mixing types within the same variable (whether a container or basic type) is not really supported by the languauge.
If these different types are related - eg different derived classes from a base class then this is fairly simple. If the types are not related then it is a whole different scenario. The easiest way to do this is to use Boost any or Boost variant. If you're not using Boost then you'll need to 'roll your own'. This is doable but non-trivial.
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
I don't know anything about boost ...
I don't it either...
The main point is you cannot "make vector which can hold any data type". Period.
You could, however, make vector which can hold pointers to "any data type" if you cast these pointers to something like void*
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
VictorN
I don't it either...
The main point is you cannot "make vector which can hold any data type". Period.
You could, however, make vector which can hold pointers to "any data type" if you cast these pointers to something like void*
and some way of knowing to what data type to re-cast these void* pointers when they are needed to be used.
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
2kaud
and some way of knowing to what data type to re-cast these void* pointers when they are needed to be used.
Tell me how please with the code example
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
Tell me how please with the code example
Possibly in the next couple of days when I have some time I'll take a look.
But you haven't answered my question in post #4. Why do you want to this? What are you trying to accomplish?
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
template<typename T>
int main()
{
vector<T> exp;
return 0;
}
templatizing main() ? ? ?
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
OReubens
templatizing main() ? ? ?
That's how we create templates I guess but I need a vector which supports all data types and can store them
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
... I need a vector which supports all data types and can store them
Then, please, explain why you need "a vector which supports all data types".
What are these types?
And, BTW, what linker errors did you get?
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
That's how we create templates I guess but I need a vector which supports all data types and can store them
main() cannot be used with a template - hence the errors!
but you still haven't explained why and what you are trying to achieve. :(
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
2kaud
main() cannot be used with a template - hence the errors!
but you still haven't explained why and what you are trying to achieve. :(
I think it's okay to use templates with main() function. I don't know if there is any limitations on where I can use it.
Anyways I'm trying to read a mathematical expression into a vector. First I take the expression into a string for example
(5+5)*9
As you can see there are two data types in this expression the first is double or int for numbers and second is char for operators like +, -, %, (,/ etc. Copying the string into vector is not a problem nor converting the string numbers into double. The problem is vector can only hold data of same type hence I was trying to use templates with vector so that I can use it to store both numbers and characters. That's all I wanna do to store elements of different data types into single vector
-
Re: Vector of Templates Problem
What you are trying to do is parse an expression and then evaluate it. There are various ways of doing this in c++ but none require a vector that holds different types.
Have a look at
http://stackoverflow.com/questions/6...pressions-in-c
http://stackoverflow.com/questions/1...ression-in-c-c
http://stackoverflow.com/questions/1...ression-parser
http://www.speqmath.com/tutorials/ex...cpp/index.html
and associated links
There are also numerous other internet sites that explain expression parsing and evaluation - just do a search.
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
2kaud
Thank you so much. I really appreciate it so that means vector which can hold any data type will maybe come in the future versions of c++.
-
Re: Vector of Templates Problem
Quote:
vector which can hold any data type will maybe come in the future versions of c+
I very much doubt it as c++ is a strongly typed language (boost::any and boost::variant are not part of the standard). c++ is defined by an ISO standard which is established via a c++ standards committee. Go ahead, try making this suggestion to them...... :D
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
I want to create a vector of templates but I'm getting 2 linker errors.
You need to add an include guard. Otherwise the compiler will generate a function in each compilation unit. The linker will then find several versions of the same function.
One way (if you use Microsoft VC++) is to add
#pragma once
at the top of the file where the template is defined. Another more general way is this,
Code:
#ifndef SOME_UNIQUE_NAME
#define SOME_UNIQUE_NAME
// template definitions put here (and any other definitions that should appear once only)
#endif
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
2kaud
Go ahead, try making this suggestion to them......
It appears to have been submitted already,
http://www.open-std.org/jtc1/sc22/wg...013/n3804.html
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
so that means vector which can hold any data type will maybe come in the future versions of c++.
You can store any pointer data type already in a vector since you can upcast any pointer to void*. And you can always downcast a void* pointer again to its actual type although this will not be typesafe (it can result in a runtime error if you downcast to the wrong type).
This is the case now and it will be so in the future. Note that boost::any does not change this even if it's include in the C++ standard.
Your problem can easily be handled by way of the so called Visitor design pattern. It's the object oriented way to accomplish typesafe downcasts.
But you can also use the (maybe more straight forward) void* way. All you need to do is associating each void* pointer with a tag indicating the actual type (and then use the tag information to downcast each void* to its actual type when needed).
So this is what you would store in a vector in principle,
Code:
struct Any {
int tag; // does not have to be an int but should indicate the actual type of ptr
void* ptr; // ptr is downcasted before use to the actual type using the tag info
};
//
std::vector<Any> anyVec;
Finally, with proper design you rarely need to resort to any kind of downcasting at all. (In fact every downcast is best viewed as a design failure and you should go to great lengths to remove them.)
-
Re: Vector of Templates Problem
Wow Thank u all for their great suggestions. It's like there are hundreds of ways to accomplish the same task and I'm just getting started lol
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
Wow Thank u all for their great suggestions. It's like there are hundreds of ways to accomplish the same task and I'm just getting started lol
Well, an include guard will take care of the linker problem.
Regarding many types in a vector I strongly advice you to seek a solution that avoids downcasting. The most common way is to declare the vector to be of a base type and then store objects derived from that base type in the vector (or rather pointers to the objects, preferably smart pointers). This is called a polymorphic design and it's an important part of object orientation (OO).
So there is just one way to do this really. :)
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
razzle
Well, an include guard will take care of the linker problem.
Regarding many types in a vector I strongly advice you to seek a solution that avoids downcasting. The most common way is to declare the vector to be of a base type and then store objects derived from that base type in the vector (or rather pointers to the objects). This is called a polymorphic design and it's an important part of object orientation (OO).
So there is just one way to do this really. :)
I think I should write a script to thank every post since all of them are really helpful lol. Now I'm confused which method should I persuade.
-
Re: Vector of Templates Problem
Quote:
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.
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
OReubens
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.
So vector of templates is out of the picture. I know Python has something like that and thought maybe c++ also had some way to do it but I don't want to rely on boost so I think I'll stick with the class token method for now and then learn expression parsing algorithms. Thank you for clearing that up.
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
So vector of templates is out of the picture. I know Python has something like that
Could you please be more specfic.
What is this feature of Python you are looking for in C++?
I can tell you right now that if you give up information there is no way of getting it back unless you store it somewhere. And that is regardless of programming language.
-
Re: Vector of Templates Problem
Quote:
Originally Posted by
razzle
Could you please be more specfic.
What is this feature of Python you are looking for in C++?
I can tell you right now that if you give up information there is no way of getting it back unless you store it somewhere. And that is regardless of programming language.
I don't remember it cause I saw it in a forum. Anyways my question has been answered and I no longer seek a vector of templates. Thank you