Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are structured bindings reusable?

I'm using Windows 10, Visual Studio 2017 v15.7.1 with /std:c++latest /permissive-

This simple code with structured bindings won't compile:

auto [a, b] = func1(x, y, z); // auto func1() -> std::tuple<double, double, double>
[a, b] = func2(x, y, z); // same signature as func2

saying E1277 attributes are not allowed here.

Code below won't compile either, same error

double a, b;
[a, b] = func1(x, y, z);
[a, b] = func2(x, y, z);

Code

auto [a, b] = func1(x, y, z);
auto [a, b] = func2(x, y, z);

won't compile as well, rightfully complaining about redefinition.

The only way it compiles is

auto [a1, b1] = func1(x, y, z);
auto [a2, b2] = func2(x, y, z);

which is, frankly, ugly.

Is this feature designed that way? Or is it VC++ bug?

like image 872
Severin Pappadeux Avatar asked May 14 '18 03:05

Severin Pappadeux


1 Answers

Structured bindings must have the auto. From cppreference:

attr(optional) cv-auto ref-operator(optional) [ identifier-list ] = expression ;

...
cv-auto - possibly cv-qualified type specifier auto
...

variants omitted; just changes the = expression part

We can see that the cv-auto is mandatory.


If you want to rebind a and b, use std::tie:

auto [a, b] = func1(x, y, z);
std::tie(a, b) = func2(x, y, z);
like image 183
Justin Avatar answered Sep 17 '22 15:09

Justin