Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapping Data types

Status
Not open for further replies.

dchinom

Programmer
Joined
Dec 14, 2005
Messages
2
Location
US
Hi,

Is there a way to map a data type (e.g. int, float, ...) to an arbitrary value, such as an enuermation.

Idealy, what I am going for is a method by which I can supply an enumeration value (or an int) and get back a typename for a particular data type which can be used to create a variable of that type.

For example:
TYPE_MAP(CORBA::tk_float)::DataType MyLocalVariable;
MyLocalVariable = 3.4;

Is this, or anything like this possible in C++? (Maybe with macros?)


Regards,
Doug M.
 
I don't think a macro would work since it would have to know the DataType at compile-time.

It might be possible with class templates... but I'm too sick to think straight right now.

I think there should be a better solution than either of these methods though.
 
You could probably do this using a function that takes your enumeration or int or string or whatever the key is, then returns a boost::any or boost::variant holding the appropriate type of data.

If there is a specific set of types that you will be using, then you could use an enumeration and a simple switch statement, setting the boost::variant to the appropriate value.

If you don't know what types will be possible, you might have to use typeid to get the correct type, and boost::any to store it.

It really dependson the details of how you plan on using it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top