c++ - 基于自动范围的结构化绑定(bind)与 vector

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

我正在尝试循环一个元组 vector :

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

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

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

但是 Visual Studio 2017 15.3.5 报错:

cannot deduce 'auto' type (initializer required)

但以下确实有效:

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

这是为什么?

最佳答案

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

因此,即使编辑器中显示红线和错误,它也会使用 ISO C++17 标准 (/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\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

关于c++ - 基于自动范围的结构化绑定(bind)与 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46571786/

相关文章:

c++ - 将 select() 与 OpenSSL 套接字一起使用?

c++ - c++14 std::experimental::filesystem::v1 和 c++17 std::filesystem 之间的区别?

c++ - 在 C++17 中解压可变元组

c++ - 未知自定义结构上的结构化绑定(bind)

.net - 从一个纯 Win32 项目的项目中使用/clr 从一个 Win32 .lib 项目调用一个函数,没有 clr

c++ - cout 是将 bool 视为整数还是将整数视为 bool?

c# - 如何在 MessageBox 上显示 FARPROC 内存地址

c++ - 多重继承 : cannot instantiate abstract class

c++ - 使用变量的类型作为模板参数