Boost logo

Boost Users :

From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2005-02-01 10:49:48

> > 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, kalb at, bjorn.karlsson at, gregod at, wekempf at