|
Boost Users : |
From: Eric Friedman (ebf_at_[hidden])
Date: 2005-02-02 16:32:40
Hi,
I can imagine cases where the current behavior might be preferable
(e.g., uses of variant types in generic contexts). But I think for the
vast majority of cases, and especially in non-generic contexts, checking
the type as you propose is a good idea.
I will determine a way to work this into the library, either by
modifying the semantics of boost::get or by introducing a new accessor
function. Thanks for the input.
Eric Friedman
Co-author of Variant library
Yuval Ronen wrote:
>>>Hi.
>>>I encountered the need to query a boost::variant to which type it
>>>currently contain. I'm talking about something like:
>>>
>>>variant<int, string> v(3);
>>>assert(v.is_a<int>());
>>>assert(!v.is_a<string>());
>>
>>Take a look at get(), it's almost what you need. Unfortunately, it
>>compiles even when the type cannot be inside the variant. This can be
>>fixed with mpl::contains:
>>
>>template<typename T, typename Variant>
>>bool variant_is_a(const Variant& v)
>>{
>> BOOST_MPL_ASSERT(( boost::mpl::contains<typename
>
> Variant::types,T> ));
>
>> return boost::get<T>(&v) != 0;
>>}
>>
>>There should be a version of get that works as variant_is_a above. IMHO,
>>the current behaviour is more dangerous than that of variant_is_a, so
>>get() should be changed and maybe a new function be created that keeps the
>>current behaviour. Some code may break with this change with a compile
>>error that would go away if the other version of get is used.
>
>
> Your code works beautifully. I really hope this feature will find its way to
> the variant library.
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