Boost logo

Boost Users :

From: Andrew Chapman (andrew.chapman_at_[hidden])
Date: 2004-02-17 10:09:02


Hey everyone,

I was just having a look at the new boost::variant vlass, and I was
expecting to be able to do something like:

     boost::variant<int, float> var;
     var = 5;

     int i = boost::get<int>(var); // fine
     float f = boost::get<float>(var); // throws

As the comments show, asking for the variant's value using get<int>
works fine but get<float> throws an exception as it is an unmatched type.

So it seems that before you can retrieve the value of the variant you
need to know its type? How do you check what type the variant is
currently holding?

This would work but is inefficient/ugly:

     boost::variant<int, float> var;
     var = 5;

     std::stringstream str;
     str << var;

     int i = atoi(str.str());
     float f = atof(str.str());

I was hoping to use the variant class to implement scripting language
style variables where you can get/set them to be any type (which seems
to work fine) and also retrieve them to any type (though retrieving a
string as a bool may not always make too much sense).

Any suggestions?

-- 
Andrew Chapman
3D Technical Director - Framestore CFC

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net