0

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?

Constructor
  • 7,273
  • 2
  • 24
  • 66
stett
  • 1,351
  • 1
  • 11
  • 24

1 Answers1

4

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
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • +1 for a moment I didn't see the base-expansion (don't ask; its late here). Nicely crafted. – WhozCraig Mar 20 '14 at 08:39
  • Cool - so the `are_base_of` struct recursively inherits from `is_base_of` for every type in `Ts`. Thanks Jarod! – stett Mar 20 '14 at 10:50