I have a lambda that ignores its int
parameter and always returns a constant.
If I mark it consteval
, compilation fails because.
The compiler complains about invoking the consteval
lambda with a non-const parameter.
But what does the parameter has to do with the lambda?
From CompilerExplorer:
source:3:16: error: the value of 'i' is not usable in a constant expression 5 | lambda(i);
void bar (auto lambda, int start, int end) {
for (int i=start; i<end; ++i) {
lambda(i);
}
}
int main( )
{
auto foo = [] (int) consteval { return 2;};
bar(foo, 1, 9);
return 0;
}
One way to solve this(and the simplest) is to change the parameter type of the lambda to
int&
so that it doesn't need to read the value, as shown below:Working demo
Here is another contrived example that has similar behavior:
Contrived example demo