简体   繁体   中英

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM