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

Auto range based structured bindings with vector

我正在尝试遍历元组向量:

std::vector<std::tuple<int, int, int>> tupleList;

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

for (auto&& [x, y, z] : tupleList) {}

但是Visual Studio2017 15.3.5报错:

cannot deduce 'auto' type (initializer required)

但以下确实有效:

for (auto&& i : tupleList) {
    auto [x, y, z] = i;
}

这是为什么?

它确实有效,但智能感知不使用相同的编译器:

因此,即使编辑器中显示红线和错误,它也会使用 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) {}
}

Visual Studio版本:

Microsoft Visual Studio Community 2017 Preview (2) Version 15.4.0 Preview 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.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