我如何编写模板或 constexpr
代码,使 match
只有在 Ts
包含 A的实例时才为真
?
template <std::uint32_t, int, int>
struct A;
template <typename... Ts>
struct X
{
constexpr bool match = ???;
};
最佳答案
写一个特质:
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>...>;
};
参见 cppreference用于在 C++11 中实现 std::disjunction
。
关于c++ - 将模板参数与模板类型匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560283/