10

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

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

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

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

但是 Visual Studio 2017 15.3.5 给出了错误:

无法推断“自动”类型(需要初始化程序)

但以下确实有效:

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

这是为什么?

4

1 回答 1

14

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

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

视觉工作室版本:

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\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
于 2017-10-04T18:39:12.770 回答