Consider the following program:
template <unsigned int I>
int f(int x)
{
auto task = [&]() { ++x; };
if constexpr (I == 0) {
task();
}
return x;
}
int main()
{
f<1>(3);
}
Compiling on gcc 9.3 with -std=c++17 -Wall -pedantic
, it issues a warning
warning: variable 'task' set but not used [-Wunused-but-set-variable]
4 | auto task = [&]() { ++x; };
But with a newer gcc version, no such warning appears. Notice that according to the manual, -Wunused-but-set-variable
is enabled by -Wall
.
Also with clang, no such a warning appears.
Test it on godbolt.
Is that a compiler shortcoming, or is this behavior (the lack of warning) wanted/expected?