Search code examples
c++boosttype-traitsc++98boost-variant

How to check if a template type is one of the types of a variant type?


Considering a variant type and a template function, how can I check the template type is one of the types of the variant ? Is there a more elegant way than the following ?

typedef boost::variant<Foo,Bar> Var;

template <typename T>
void f(const T& x)
{
  BOOST_STATIC_ASSERT(
       boost::is_same<T,Foo>::value
    || boost::is_same<T,Bar>::value
  );
}

Note : I use Boost 1.57 and gcc 4.8.3. I don't use C++11 for compatibility with old gcc versions.


Solution

  • 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

    or manually iterate over 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