Is there a way to negate constrains?

75 Views Asked by At

How does one implement NOT such that fn accepts Y but not X, without creating multiple concepts and chaining them?

template<class T>
concept Fooable = requires(T t) {
    { t.foo() };
    NOT({ t.bar()}); // imaginary syntax
};

struct Y {
    void foo() {}
};

struct X {
    void foo() {}
    void bar() {}
};


template <Fooable T>
void fn(T t) {}
1

There are 1 best solutions below

0
康桓瑋 On BEST ANSWER

You can use NOT with the requires-clause as it yields a bool:

template<class T>
concept Fooable = requires(T t) {
    t.foo();
    requires (!requires { t.bar(); });
};

Demo