C++ C++;带-Wshadow警告的结构化绑定?
更新:gcc可以工作,但不能发出叮当声C++ C++;带-Wshadow警告的结构化绑定?,c++,gcc-warning,stdtuple,C++,Gcc Warning,Stdtuple,更新:gcc可以工作,但不能发出叮当声 用C++17编译 GCC/Clang-Wshadow将在出现阴影局部变量时发出警告,但对于结构化绑定,此标志不起作用。如何在这种情况下暴露警告 std::tuple<int, int> yy = {-1, -2}; int x = 1; { // int x = 2; // will warn -Wshadow auto [x, y] = yy; // will not warn even if compile with
用C++17编译 GCC/Clang-Wshadow将在出现阴影局部变量时发出警告,但对于结构化绑定,此标志不起作用。如何在这种情况下暴露警告
std::tuple<int, int> yy = {-1, -2};
int x = 1;
{
// int x = 2; // will warn -Wshadow
auto [x, y] = yy; // will not warn even if compile with -weverything
}
std::tuple yy={-1,-2};
int x=1;
{
//int x=2;//将发出警告-Wshadow
auto[x,y]=yy;//即使使用-weverything编译,也不会发出警告
}
我已经用gcc 9.2和clang 9.0.0试用了您的示例。这是我的最小程序:
#包括
std::tuple yy={-1,-2};
无效bla(int x)
{
if(x)
{
自动[x,y]=yy;
}
}
获取gcc中的阴影警告(-Wall-Wshadow-std=c++17
):
这是一个叮当作响的bug,如本文所述:。编译字符串是什么样子的?您正在使用
-std=c++17
(或更高版本)?由于您可以从结构化绑定中使用x
,因此我预期会出现阴影警告。请提供。
<source>: In function 'void bla(int)':
<source>:8:19: warning: declaration of 'auto x' shadows a parameter [-Wshadow]
8 | auto [x, y] = yy;
| ^
<source>:4:14: note: shadowed declaration is here
4 | void bla(int x)
| ~~~~^
<source>:8:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
8 | auto [x, y] = yy;
| ^~~~~~
<source>:8:14: warning: unused variable '[x, y]' [-Wunused-variable]
auto [x, y] = yy;
^
1 warning generated.