using same datatype in boost variant
Can we explicitly typecast the value which is to be 开发者_高级运维stored in boost varaint??
Example:
typedef int abc;
typedef int asd;
typedef boost::variant<abc, char, asd, float> link_try1;
int main()
{
link_try1 qw;
qw = static_cast<asd>(1234);
printf("value of which is:%d", qw.which());
return 0;
}
Here I want the which() function to retrun 3 but it always retruns 0. Is there a way of directly changing the value in which_ (private variable in class variant) or explicitly specifying the datatype to be used??
Regards Ankith
It is possible, but it won't work as expected.
The key idea, on a variant, is that the type acts as a key. When you actually request a given type (using boost::get
or visitation), the first type in the variant
that matches the key is elected, thus here asd
would be haughtily ignored.
If you need to store several integers for different purposes, you can use BOOST_STRONG_TYPEDEF
to create different integer-like classes and use those in the variant
.
精彩评论