tags:

views:

71

answers:

4

I need to create a map that can contain as its value any basic data type such as double,float,string, char etc... to store values from a request for a server component.

I was thinking of using a map such as this: std::map<std::string, void*>, however I don't know that this is a very good solution.

I wondered if anyone can advice on a better solution. The main point is that the values have to be in the same map.

Please advice?

A: 

You could have a struct or union (which has an int field, float field, etc) be the value type for the map. This would be much clearer and more maintainable.

Brian Clements
+13  A: 

You could use boost::any as the value which can hold "any" type.

(It is better to use boost::variant if there are only finitely many "basic data types")

KennyTM
+4  A: 

Try boost::any or boost::variant for the members of the map.

Mark Ransom
A: 

It sounds like you basically need the same service as provided by Qt's QVariant type. I don't recommend hauling in Qt for just this one class, but you might want to look at the API and see if you want to create something like it.

Docs: http://doc.trolltech.com/4.2/qvariant.html

jkerian