将模板参数与模板类型匹配

Gra*_*rak 9 c++ templates c++11

我如何编写模板或constexpr代码,match只有在Ts包含实例的情况下才是真的A

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

template <typename... Ts>
struct X
{
    constexpr bool match = ???;
};
Run Code Online (Sandbox Code Playgroud)

T.C*_*.C. 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 {};
Run Code Online (Sandbox Code Playgroud)

然后使用它:

template <typename... Ts>
struct X
{
    constexpr bool match = std::disjunction_v<is_A<Ts>...>;
};
Run Code Online (Sandbox Code Playgroud)

有关C++ 11中的实现,请参阅cppreferencestd::disjunction.

  • 感谢您介绍std :: disjunction :) (2认同)