11

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

2
  • Why don't you just write a normal functor? Nov 7, 2015 at 21:07
  • 4
    The Concepts TS does not modify the grammar for lambdas, so I think it is not possible to append a requires-clause to a lambda. By altering the definition of simple-type-specifier, it allows to use constrained-type-specifiers as parameters.
    – dyp
    Nov 7, 2015 at 21:08

2 Answers 2

7

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

4
  • Yes, looks like that's true. I found this thread on the concepts group that addresses the same question. It's unfortunate as there are lots of cases where the concise concepts syntax isn't sufficient to express the constraint needed. Hopefully, this will be addressed in a future draft.
    – Ryan Burn
    Nov 7, 2015 at 21:44
  • Right - I'm aware of the concise concepts syntax. Still it's IMO a defect that you would have to define a completely new concept if you have a more complicated requires clause. Furthermore, that syntax won't work if you have a concept that needs to reference multiple arguments.
    – Ryan Burn
    Nov 7, 2015 at 22:05
  • @rnickb based on the link you posted it's not on Adrew Shutton's priorities list "Making lambdas bigger was not high on our list of priorities (lambdas are supposed to make code shorter).". I kinda agree with it. Lambdas are indeed supposed to make code shorter. On the other hand what you're saying is true, composition of concepts to accomplish what you want, kinda obfuscates the code-base. I guess we'll see in the future what happens :).
    – 101010
    Nov 7, 2015 at 22:13
  • 2
    requires C1<decltype(x)> && C2<decltype(x)> is a requires-clause, not a requires-expression.
    – T.C.
    Nov 7, 2015 at 22:40
2

Since this question can appear as the first result of some searches¹, it is worth pointing out that adding constraints to the operator() of the closure type with requires is possible as of C++20.


¹ At least by typing “c++ lambda function requires” to Google

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.