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

时间:2017-10-04 18:28:59

标签: c++ c++17 structured-bindings

我试图遍历元组的向量:

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

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

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

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

  

不能推断出&#39; auto&#39;类型(需要初始化程序)

但以下确实有效:

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

为什么?

1 个答案:

答案 0 :(得分:14)

它确实有效,但intellisense不使用相同的编译器: enter image description here

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

我编写了以下程序:

>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

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

从命令行:

{{1}}
相关问题