C ++: check if pattern type is one of variational pattern types

Say we have a function:

template <typename Kind, typename... Kinds> void foo(){...};

What is the easiest way to check if a View type is one of the Views types in C ++ (including C ++ 1z)?

+4
source share
1 answer

You can use the following type trait:

template <typename...>
struct is_one_of {
    static constexpr bool value = false;
};

template <typename F, typename S, typename... T>
struct is_one_of<F, S, T...> {
    static constexpr bool value =
        std::is_same<F, S>::value || is_one_of<F, T...>::value;
};

Live demo

+3
source

Source: https://habr.com/ru/post/1618736/


All Articles