基于自动范围的结构化绑定与矢量

Eej*_*jin 9 c++ c++17 structured-bindings

我试图循环一个元组的向量:

std::vector<std::tuple<int, int, int>> tupleList;
Run Code Online (Sandbox Code Playgroud)

通过使用基于范围的for循环与结构化绑定:

for (auto&& [x, y, z] : tupleList) {}
Run Code Online (Sandbox Code Playgroud)

但Visual Studio 2017 15.3.5给出了错误:

无法推断'auto'类型(需要初始化程序)

但以下确实有效:

for (auto&& i : tupleList) {
    auto [x, y, z] = i;
}
Run Code Online (Sandbox Code Playgroud)

这是为什么?

wal*_*lly 14

它确实有效,但intellisense不使用相同的编译器: 在此输入图像描述

因此,即使编辑器中显示红线和错误,它也会使用ISO C++17 Standard (/std:c++17)开关进行编译.

我编译了以下程序:

#include <vector>
#include <tuple>

std::vector<std::tuple<int, int, int>> tupleList;
//By using a range based for loop with structured bindings :

int main()
{
    for(auto&&[x, y, z] : tupleList) {}
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio版本:

Microsoft Visual Studio社区2017预览版(2)版本15.4.0预览版3.0 VisualStudio.15.Preview/15.4.0-pre.3.0 + 26923.0

cl版本:

19.11.25547.0

从命令行:

>cl test.cpp /std:c++17
Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25547 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
C:\Program Files (x86)\Microsoft Visual Studio\Preview\Community\VC\Tools\MSVC\14.11.25503\include\cstddef(31): warning C4577: 'noexcept' used with no exception handling mode specified; termination on exception is not guaranteed. Specify /EHsc
Microsoft (R) Incremental Linker Version 14.11.25547.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj
Run Code Online (Sandbox Code Playgroud)