Search code examples
c++templatesc++11variadic-templatesenable-if

Using conditional definitions with variadic templates


If I have a template function which takes a known number of template arguments, I can use the enable_if statement along with things like is_base_of to limit the legal types. For example:

template <typename T>
typename enable_if<is_base_of<BaseClass, T>::value, void>::type
function() {
    // do stuff with T, which is ensured to be a child class of BaseClass
}

Now suppose we want to do the same thing for a variadic template - check to make sure all types are children of a specific base class.

template <typename... T>
/* What goes here? */
function() {
    // do stuff with the T types, which are ensured to be child classes of BaseClass
}

How would you go about writing this conditional definition?


Solution

  • You may use the following type_traits:

    template <typename Base, typename T, typename... Ts>
    struct are_base_of :
        std::conditional<std::is_base_of<Base, T>::value, are_base_of<Base, Ts...>,
                         std::false_type>::type
    {};
    
    template <typename Base, typename T>
    struct are_base_of<Base, T> : std::is_base_of<Base, T> {};
    

    And then use

    template <typename... Ts>
    typename std::enable_if<are_base_of<BaseClass, Ts...>::value, void>::type
    function() {
        // do stuff with Ts, which is ensured to be a child class of BaseClass
    }