2016-06-01 68 views
9

如何编写模板或constexpr代码,以使match只有在Ts包含A的实例时才为true?将模板参数与模板类型匹配

template <std::uint32_t, int, int> 
struct A; 

template <typename... Ts> 
struct X 
{ 
    constexpr bool match = ???; 
}; 

回答

10

写特质:

template<class> 
struct is_A : std::false_type {}; 

template<std::uint32_t X, int Y, int Z> 
struct is_A<A<X,Y,Z>> : std::true_type {}; 

然后使用它:

template <typename... Ts> 
struct X 
{ 
    constexpr bool match = std::disjunction_v<is_A<Ts>...>; 
}; 

std::disjunction在C++ 11的实现见cppreference

+2

感谢您介绍std :: disjunction :) – Arunmu

相关问题