Hi. I encountered the need to query a boost::variant to which type it currently contain. I'm talking about something like:
variant
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
bool variant_is_a(const Variant& v) { BOOST_MPL_ASSERT(( boost::mpl::contains )); 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.