c++ - 将模板参数与模板类型匹配

标签 c++ templates c++11

我如何编写模板或 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/

相关文章:

c++ - 表示 "something callable with particular signature"的模板参数

C++ lambda 表达式 - 编译器如何解释它们?

c++ - 识别模板中的原始类型

c++ - 类内私有(private)结构的构造函数出错

c++ - Efficient Ransac 的模板化内核存在问题

c++ - 从模板类中提取 typedef/using 定义

c++ - Variadic char 数组在嵌套函数 C++ 中显示不正确

c++ - 在 VS2017 中搜索内存

c++ - C 设置环境变体

c++ - 使用初始化列表时发生堆损坏错误