I'm using a nested lambda to walk through some data.
The outer lambda does some processing and then calls the inner lambda.
I get the following warning:
x86-64 clang 13.0.1 - 2629ms (104630B) ~1800 lines filtered
Output of x86-64 clang 13.0.1 (Compiler #1)
<source>:9:34: warning: class '' does not declare any constructor to initialize its non-modifiable members
const auto PickVarAtRandom = [&]<bool SATvar> {
^
<source>:9:34: note: in instantiation of member class '' requested here
<source>:18:41: note: in instantiation of function template specialization 'main()::(anonymous class)::operator()<true>' requested here
const auto result = doPick.template operator()<true>();
^
<source>:10:13: note: reference member '' will never be initialized
if (Length > 50) { printf("hallo"); return false; }
The code uses a nested lambda call. The following code will reproduce the issue in Godbolt.
#include <stdlib.h>
#include <stdio.h>
int main() {
const auto Length = rand() % 2;
//warning: class '' does not declare a constructor ...
// V
const auto PickVarAtRandom = [&]<bool SATvar> {
if (Length > 0) { printf("one %i", Length); return false; }
else { printf("zero %i", Length); }
return true;
};
const auto doPick = [&]<bool SATvar>() {
return PickVarAtRandom.template operator()<SATvar>();
};
const auto result = doPick.template operator()<true>();
}
The error goes away if just use a single lambda:
//no warning
#include <stdlib.h>
#include <stdio.h>
int main() {
const auto Length = rand() % 100;
const auto PickVarAtRandom = [&]<bool SATvar> {
if (Length > 50) { printf("hallo"); return false; }
return true;
};
const auto result = PickVarAtRandom.template operator()<true>();
}
I'm using clang 14 on MacOS, but in order to reproduce the warning in godbolt I need to select clang 13.
clang++ --version
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
Why do I get this warning with nested lambdas?
Can I suppress the warning?
Or is there a way to get rid of the warning whilst still using nested lambdas?