Hi I'm facing a inconsistent behavior with Variant when bool is defined. The below code will return i as 0 typedef boost::variant <int> VariantType; VariantType variant; int i = boost::get <int> (variant)); // default to 0 If I set a bool in the variant typedef boost::variant <bool, int> VariantType; VariantType variant; int i = boost::get <int> (variant)); // this throws fail get Is it intended behavior? Robin
AMDG Robin wrote:
I'm facing a inconsistent behavior with Variant when bool is defined. The below code will return i as 0
typedef boost::variant <int> VariantType;
VariantType variant; int i = boost::get <int> (variant)); // default to 0
If I set a bool in the variant
typedef boost::variant <bool, int> VariantType;
VariantType variant; int i = boost::get <int> (variant)); // this throws fail get
Is it intended behavior?
Yes. A default constructed variant contains the first variant type, so your variant contains a bool, not an int. In Christ, Steven Watanabe
On 05/22/10 10:54, Robin wrote: [snip]
typedef boost::variant <bool, int> VariantType;
VariantType variant; int i = boost::get <int> (variant)); // this throws fail get
Is it intended behavior?
Robin
The default value of the variant is the 1st bound type, which in case of: variant<bool,int> is bool. Hence, when you try to retrieve an int with get<int>(variant), it throws, as expected. -Larry
participants (3)
-
Larry Evans
-
Robin
-
Steven Watanabe