#include <tuple>
#include <type_traits>
template <typename T, typename Tuple>
struct has_type;
template <typename T>
struct has_type<T, std::tuple<>> : std::false_type {};
template <typename T, typename U, typename... Ts>
struct has_type<T, std::tuple<U, Ts...>> : has_type<T, std::tuple<Ts...>> {};
template <typename T, typename... Ts>
struct has_type<T, std::tuple<T, Ts...>> : std::true_type {};
DEMO
And an additional alias, if the trait itself should be std::true_type
or std::false_type
:
template <typename T, typename Tuple>
using tuple_contains_type = typename has_type<T, Tuple>::type;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…