Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check a concept against a type

How to get a core-constant expression of type bool from a concept and a type?

template<class T>
concept Valid = requires(T t) {
    { t.x };
};

struct ValidExample   { int x; };
struct InValidExample {};

static_assert(?); // ValidExample is Valid
static_assert(?); // InValidExample is not Valid

I'm starting to play with concepts, and I'd like to check a type against a concept (to be more precise: to define a trait from a concept). How to do so?

like image 821
YSC Avatar asked Apr 23 '19 12:04

YSC


1 Answers

You might use:

static_assert(Valid<ValidExample>); // ValidExample is Valid
static_assert(!Valid<InValidExample>); // InValidExample is not Valid

as template variables (of type bool).

like image 188
Jarod42 Avatar answered Nov 17 '22 16:11

Jarod42