Search code examples
c++type-traitsc++-concepts

C++ concept to type trait


I have a concept as follows:

template<class T>
concept Reader = requires(T a)
{...};

I want to have a type trait which checks if a class conforms to the Reader concept - Something like is_reader<myclass>::value. How do I achieve this?

My attempts till now has been something like:

template<typename T>
struct is_reader : std::false_type;

template<Reader T>
struct is_reader : std::true_type;

This doesn't compile due to re-declaration of template type.

I also tried:

template<typename T>
struct is_reader : std::false_type;

template<>
struct is_reader<Reader auto> : std::true_type;

While this compiles, it does not work.

How to make this work? Also why does my second try not work - What is it doing?


Solution

  • The correct way is

    template<class T>
    concept Reader = requires(T a) {...};
    
    template<typename T>
    struct is_reader : std::false_type {};
    
    template<Reader T>
    struct is_reader<T> : std::true_type {};