[英]gcc >=10.1 and clang do not detect unused but set lambda
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使用
-std=c++17 -Wall -pedantic
在 gcc 9.3 上编译,它会发出警告
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.但是对于较新的 gcc 版本,则不会出现此类警告。 Notice that according to the manual ,
-Wunused-but-set-variable
is enabled by -Wall
.请注意,根据手册,
-Wunused-but-set-variable
由-Wall
启用。
Also with clang, no such a warning appears.同样使用 clang,不会出现这样的警告。
Test it on godbolt .在godbolt上测试它。
Is that a compiler shortcoming, or is this behavior (the lack of warning) wanted/expected?这是编译器的缺点,还是需要/预期这种行为(缺乏警告)?
Consider this slightly modifed code:考虑这个稍微修改过的代码:
template <unsigned int I>
int f(int x)
{
auto task = [&]() { ++x; };
if constexpr (I == 0) {
task();
}
return x;
}
int main()
{
f<0>(3);
f<1>(3);
}
With gcc 9.3 -std=c++2a -Wall -Werror
you get an error (warning treated as error) :使用gcc 9.3
-std=c++2a -Wall -Werror
你会得到一个错误(警告被视为错误) :
<source>: In instantiation of 'int f(int) [with unsigned int I = 1]':
<source>:14:11: required from here
<source>:4:10: error: variable 'task' set but not used [-Werror=unused-but-set-variable]
4 | auto task = [&]() { ++x; };
| ^~~~
cc1plus: all warnings being treated as errors
This is bad, because the code is completely fine and it can be considered a bug that it triggers a warning.这很糟糕,因为代码完全没问题,它可以被认为是触发警告的错误。 Apparently this has been fixed in gcc >= 10.1.
显然,这已在 gcc >= 10.1 中修复。
One could argue that one could/should move declaration of task
into the if constexpr
branch, but then consider that this made gcc issue a warning as well:有人可能会争辩说,可以/应该将
task
声明移入if constexpr
分支,但随后考虑这会使 gcc 也发出警告:
template <unsigned int I>
int f(int x)
{
auto task = [&]() { ++x; };
if constexpr (I == 0) {
task();
} else if constexpr (I == 1) {
task();
}
return x;
}
int main()
{
f<0>(3);
f<1>(3);
f<2>(3);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.