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;
}
int&
works because withint&
it doesn't need to read the value. – Gavelconst int&
as well, in case you don't want a mutable reference. – Adalbert