templates in a std::vector??

B

Bart Kevelham

Hi all,

I have the following problem
I need to store a list of simple objects. The general form of these
objects is:
- std::string type
- std::string name
- value

The problem lies within the value. This can be of any type. A
std::string or an openGL float4 or whatever. This could easily be
implemented with a template, but the problem is this: I have to store
instances of this object in something like a stl vector or list
(Anything that is growable). So the final list can contain one instance
whose value is a string and another instance whose value is an integer
(for example). I can't figure out a way to do this. Does anyone of you
guys/girls have an idea?

Kind regards,

Bart
 
T

Thomas Maier-Komor

Bart said:
Hi all,

I have the following problem
I need to store a list of simple objects. The general form of these
objects is:
- std::string type
- std::string name
- value

The problem lies within the value. This can be of any type. A
std::string or an openGL float4 or whatever. This could easily be
implemented with a template, but the problem is this: I have to store
instances of this object in something like a stl vector or list
(Anything that is growable). So the final list can contain one instance
whose value is a string and another instance whose value is an integer
(for example). I can't figure out a way to do this. Does anyone of you
guys/girls have an idea?

Kind regards,

Bart

make a class B with type and name and derive from it for the different
value types. Then instead of using a vector<B>, use a vector<B *>.
Remember that you cannot store objects of different size within
a vector, which also effectively disables assigning a derivation of B
to an element of a vector<B>, as only the B part will be assigned
omitting the extensions made during derivation...
 
J

John Harrison

Bart Kevelham said:
Hi all,

I have the following problem
I need to store a list of simple objects. The general form of these
objects is:
- std::string type
- std::string name
- value

The problem lies within the value. This can be of any type. A
std::string or an openGL float4 or whatever. This could easily be
implemented with a template, but the problem is this: I have to store
instances of this object in something like a stl vector or list
(Anything that is growable). So the final list can contain one instance
whose value is a string and another instance whose value is an integer
(for example). I can't figure out a way to do this. Does anyone of you
guys/girls have an idea?

Kind regards,

Bart

Thomas' suggestion is one way to go. The other is to use a class that has
been written to hold a variety of types. Implementing this is tricky but
fortunately its already been done for you. Check out the boost::any class
http://www.boost.org/doc/html/any.html and the boost::variant class
http://www.boost.org/doc/html/variant.html

john
 
B

Bart Kevelham

B

Brian Riis

Bart said:
Hey cool, I didn't know about that. Boost keeps amazing me. :)

Thanks (and also a "thank you" for Thomas of course),

Bart

For an enjoyable introduction, ask the Guru!

http://www.cuj.com/documents/s=7988/cujcexp1912hyslop/hyslop.htm

If you don't know the Guru, get to know her! In other words, dig into
Herb Sutter's and Jim Hoslyp's recurring "Conversations" column at
www.cuj.com

Browse through the experts forum. Even if you know the subject of the
article, the Conversations are always fun to read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top