Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = ???;
};
like image 212
Graznarak Avatar asked Jun 01 '16 05:06

Graznarak


People also ask

Can template parameter be a template?

Another parameter we can pass is a template (as opposed to a type). This means that the parameter we pass is itself a template: template<template <typename T> typename Templ> class MyTemplateTemplateClass { // ... };

What is template type parameter?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

Why do we use template template parameter?

8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.

Which parameter is allowed for non-type template?

A non-type template parameter must have a structural type, which is one of the following types (optionally cv-qualified, the qualifiers are ignored): lvalue reference type (to object or to function); an integral type; a pointer type (to object or to function);


1 Answers

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.

like image 85
T.C. Avatar answered Oct 22 '22 07:10

T.C.