Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a requires clause with lambda functor arguments?

Is there any way to apply a general requires clause to the arguments of a lambda functor?

Suppose I have two constraints C1 and C2 that I want checked against an argument. I would have expected the following to work since a similar syntax is allowed for functions:

[](auto x) requires C1<decltype(x)> && C2<decltype(x)> {
    // ...
}

But this won't compile with GCC 6

like image 866
Ryan Burn Avatar asked Nov 07 '15 20:11

Ryan Burn


1 Answers

In my humble opinion and based on Concepts TS §5.1.4/c4 Requires expressions [expr.prim.req] (Emphasis Mine):

A requires-expression shall appear only within a concept definition (7.1.7), or within the requires-clause of a template-declaration (Clause 14) or function declaration (8.3.5).

The above quote specifically dictates the contexts where a requires clause can appear and lambdas is not one of them.

Concequently,

[](auto x) requires C1<decltype(x)> && C2<decltype(x)> {
    // ...
}

Is not valid.

However, in §5.1.2 Lambda expressions [expr.prim.lambda] there's the following example:

template<typename T> concept bool C = true;
auto gl = [](C& a, C* b) { a = *b; }; // OK: denotes a generic lambda

So I guess, you could accomplish what you want in the following manner:

template <class T> concept bool C1 = true;                                        
template <class T> concept bool C2 = true;
template <class T> concept bool C3 = C1<T> && C2<T>; // Define a concept that combines 
                                                     // `C1` and `C2` requirements.                   

int main() {                                                                      
  auto f = [](C3 x)  { /* Do what ever */ }; // OK generic lambda that requires input 
                                             // argument satisfy `C1` and `C2`                                                                                                                          
} 

Live Demo

like image 154
101010 Avatar answered Oct 24 '22 17:10

101010