Graznarak
Graznarak

Reputation: 3734

Match template parameter to template type

How would I write template or constexpr code such that match is true only if Ts contains an instance of A?

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

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

Upvotes: 9

Views: 170

Answers (1)

T.C.
T.C.

Reputation: 137425

Write a trait:

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 {};

Then use it:

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

See cppreference for an implementation of std::disjunction in C++11.

Upvotes: 10

Related Questions