Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto range based structured bindings with vector

I'm trying to loop over a vector of tuples:

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

By using a range based for loop with structured bindings:

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

But Visual Studio 2017 15.3.5 gives the error:

cannot deduce 'auto' type (initializer required)

But the following does work:

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

Why is that?

like image 959
Eejin Avatar asked Oct 04 '17 18:10

Eejin


1 Answers

It does work, but the intellisense doesn't use the same compiler: enter image description here

So even with the red lines and error shown in the editor it does compile with the ISO C++17 Standard (/std:c++17) switch.

I compiled the following program:

#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 version:

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 version:

19.11.25547.0

From command line:

>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
like image 90
wally Avatar answered Oct 18 '22 19:10

wally