Use MPL:
#include <boost/variant/variant.hpp>
#include <boost/mpl/contains.hpp>
typedef boost::variant<Foo, Bar> Var;
template <typename T>
void f(const T& x)
{
BOOST_STATIC_ASSERT(boost::mpl::contains<Var::types, T>::value);
}
DEMO http://coliru.stacked-crooked.com/a/310e43c81d2c5078
或手动迭代boost:::variant
types:
#include <boost/variant/variant_fwd.hpp>
#include <boost/type_traits.hpp>
template <typename T, typename V>
struct variant_has_type;
template <typename T, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename Ts)>
struct variant_has_type<T, boost::variant<T, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(Ts)> >
: boost::true_type {};
template <typename T, typename U, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename Ts)>
struct variant_has_type<T, boost::variant<U, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(Ts)> >
: variant_has_type<T, boost::variant<BOOST_VARIANT_ENUM_SHIFTED_PARAMS(Ts), void> > {};
template <typename T, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename Ts)>
struct variant_has_type<T, boost::variant<void, BOOST_VARIANT_ENUM_SHIFTED_PARAMS(Ts)> >
: boost::false_type {};
DEMO 2 http://coliru.stacked-crooked.com/a/964a3c31c5cafdbc