c++ - 为什么 GCC 会为结构化绑定(bind)诊断未使用的变量而 Clang 不会?

标签 c++ gcc language-lawyer c++17 structured-bindings

让我们从一个最小的例子开始:

#include <utility>

int main()
{
    auto [a, b] = std::pair(1, 'A');
    return a;
}

使用 GCC 7.3 编译传递 -std=c++17-Wunused-variable,并运行它:

<source>: In function 'int main()':
<source>:5:15: warning: unused variable 'b' [-Wunused-variable]
     auto [a, b] = std::pair(1, 'A');
               ^

GCC 可能会正确地报告未使用 b,但它错误地将其称为变量。引用 [dcl.struct.bind]/1 :

A structured binding declaration introduces the identifiers v0, v1, v2, … of the identifier-list as names ([basic.scope.declarative]) of structured bindings.

所以 b 显然不是一个变量,而是一个名称。使用 Clang 6.0.0 和相同的标志编译相同的代码,我们不会收到任何警告。从代码中删除 return a; 语句:

#include <utility>

int main()
{
    auto [a, b] = std::pair(1, 'A');
    // return a;
}

并用 Clang 再次编译它,我们得到:

<source>:5:10: warning: unused variable '[a, b]' [-Wunused-variable]
    auto [a, b] = std::pair(1, 'A');
         ^

根据我的解释,正确地将 [a, b] 视为变量,将 ab 分别视为名称。 我的问题然后是为什么 GCC 会诊断出 b 未使用的警告,考虑到该变量实际上在 return a; 来自第一个代码的语句?

最佳答案

关于c++ - 为什么 GCC 会为结构化绑定(bind)诊断未使用的变量而 Clang 不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49845474/

相关文章:

c++ - C++ 编译器如何区分左移位/右移位和 ostream<</ostream>> 运算符?

gcc - 更改 GCC 的输出以进行 0-set(清除)操作

java - 有效地最终与最终 - 不同的行为

c++ - 外部结构前向声明

c++ - 将 lambda 直接传递给函数

c++ - 如何同时满足 gcc4.1.2 和 gcc 4.7.3

c++ - [错误]未分配正在释放的指针

c++ - 使用C时间函数测量时间: are they code-reordering resistant?

c++ - Operator new 返回比请求更多的内存

c++ - 具有 SDL2 问题的 Visual Studio 代码