Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 {};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading