Már*_*ldi 6 c++ gcc language-lawyer c++17 structured-bindings
让我们从一个最小的例子开始:
#include <utility>
int main()
{
auto [a, b] = std::pair(1, 'A');
return a;
}
Run Code Online (Sandbox Code Playgroud)
与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');
^
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会可能正确报告缺席使用b,但错误地将其称为变量.引用[dcl.struct.bind]/1:
结构化绑定声明将标识符列表的标识符 v0,v1,v2,... 引入为结构化绑定的名称([basic.scope.declarative]).
所以b显然不是变量,而是名称.使用Clang 6.0.0编译相同的代码,并使用相同的标志,我们不会得到任何警告.return a;从代码中删除语句:
#include <utility>
int main()
{
auto [a, b] = std::pair(1, 'A');
// return a;
}
Run Code Online (Sandbox Code Playgroud)
并使用Clang再次编译它,我们得到:
<source>:5:10: warning: unused variable '[a, b]' [-Wunused-variable]
auto [a, b] = std::pair(1, 'A');
^
Run Code Online (Sandbox Code Playgroud)
其中,从我的理解,正确对待[a, b]作为一个变量,a并b分别为只是名称.那么我的问题是为什么GCC会诊断出b未使用的警告,考虑到变量实际上是在return a;第一个代码的语句中使用的?
| 归档时间: |
|
| 查看次数: |
554 次 |
| 最近记录: |