【问题标题】:C++ Structured binding with -Wshadow warnings?带有-Wshadow警告的C ++结构化绑定?
【发布时间】:2019-09-23 06:19:21
【问题描述】:

更新: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 -weverything 
}

【问题讨论】:

标签: c++ gcc-warning stdtuple


【解决方案1】:

我已经在godbolt 上使用 gcc 9.2 和 clang 9.0.0 尝试了您的示例。那里有我的最小程序:

#include <tuple>

std::tuple<int, int> yy = {-1, -2};
void bla(int x)
{
    if (x)
    {
        auto [x, y] = yy;
    }
}

在 gcc (-Wall -Wshadow -std=c++17) 中获得影子警告:

<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;

      |              ^~~~~~

但不是clang (-Wall -Wshadow -Wshadow-all -std=c++17):

<source>:8:14: warning: unused variable '[x, y]' [-Wunused-variable]

        auto [x, y] = yy;

             ^

1 warning generated.

这是 clang 中的一个错误,如下所述:https://bugs.llvm.org/show_bug.cgi?id=40858

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-10
    • 2016-02-17
    • 1970-01-01
    • 2018-06-06
    • 2015-03-06
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多