gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:32:08 27 4
gpt4 key购买 nike

我正在尝试循环一个元组 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com