Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ requires expression with inverse return type concept check

I try to define a concept with a requires expression which checks some member function existence on the type.
The problem I run into is, that a single concept has to be plugged in for the return type check without any preceding bool operations like "not". This forces me to also write concepts for the inverse if needed, which is not really handy.
Is there something I am missing or another good way to do it?

Simple example:

template <typename T>
concept IsReference = std::is_reference_v<T>;

template <typename T>
concept HasSomeFunction = requires (T t) {
    {t.func()} -> IsReference; /* this is ok */
    {t.anotherFunc()} -> (not IsReference); /* this is not ok */
};

like image 969
Lessi Avatar asked May 27 '21 09:05

Lessi


1 Answers

Here is a syntax that works on latest gcc and clang.

template <typename T>
concept HasSomeFunction = requires (T t) {
    {t.func()} -> IsReference; /* this is ok */
    requires !IsReference<decltype(t.anotherFunc())>;
};

Demo

It a bit harder to read. Maybe someone with some more experience with concepts can provide a better solution.

like image 163
super Avatar answered Sep 30 '22 01:09

super