
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>()); This can be done using the which() method, but its drawback is that I rely on the order of the types in the variant declaration rather on the type, which is really what I'm interested in. I can implement the variant_is_a<>() feature using a visitor such as: template <typename T> struct is_a_visitor : public boost::static_visitor<bool> { template <typename U> bool operator()(const U &) const { return false; } bool operator()(const T &) const { return true; } }; template <typename T, class Variant> bool variant_is_a(const Variant &v) { return v.apply_visitor(is_a_visitor<T>()); } but this way has a problem: calling variant_is_a<double>() on a variant of type variant<int, string> (no double for this variant) will simply return false, rather than just not compile, which is what I prefer. Making this not compile will require a new feature from variant: a boolean constant 'can_be'. With this, the variant_is_a() function could be written with additional line: BOOST_STATIC_ASSERT(Variant::can_be<T>::value); IMO, the is_a/can_be features are general and useful enough to be included in the library. Wouldn't you agree? Yuval