Search code examples
c++templatesboostenable-if

C++ boost enable_if question


Do I have any way to simplify the following statements? (probably, using boost::enable_if).

I have a simple class structure - Base base class, Derived1, Derived2 inherit from Base.

I have the following code:

template <typename Y> struct translator_between<Base, Y> {
   typedef some_translator<Base, Y> type;
};

template <typename Y> struct translator_between<Derived1, Y> {
   typedef some_translator<Derived1, Y> type;
};

template <typename Y> struct translator_between<Derived2, Y> {
   typedef some_translator<Derived2, Y> type;
};

I want to write the same statement using one template specialization of translator_between.

An example (pseudocode) of what I want to be able to write:

template <typename Class, typename Y>

ONLY_INSTANTIATE_THIS_TEMPLATE_IF (Class is 'Base' or any derived from 'Base')

struct translator_between<Class, Y> {
   typedef some_translator<Class, Y> type;
};

Any way to achieve this using boost::enable_if and boost::is_base_of?


Solution

  • First, you'll have to pick your choice among:

    • is_base_of
    • is_convertible

    both can be found in <boost/type_traits.hpp>, the latter being more permissive.

    If you with to simply prevent the instantiation of this type for some combination, then use a static assert:

    // C++03
    #include <boost/mpl/assert.hpp>
    
    template <typename From, typename To>
    struct translator_between
    {
      BOOST_MPL_ASSERT((boost::is_base_of<To,From>));
      typedef translator_selector<From,To> type;
    };
    
    // C++0x
    template <typename From, typename To>
    struct translator_between
    {
      static_assert(boost::is_base_of<To,From>::value,
                    "From does not derive from To");
      typedef translator_selector<From,To> type;
    };
    

    Since there is no overload resolution taking place here, you don't need enable_if.