Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different cv-qualifiers with structured bindings

Tags:

c++

c++17

The structured binding declaration in C++17 allows for several different options, such as:

std::tuple<int, int> foo();

auto  [a, b] = foo(); // a and b are: int
const auto  [a, b] = foo(); // a and b are: int const
const auto& [a, b] = foo(); // a and b are: int const&

Is there any way to give a and b different cv-qualifiers? For example the type of a as int and b as int const?

like image 396
Jonas Avatar asked Apr 21 '17 08:04

Jonas


1 Answers

No - this is covered in the proposal's Q&A:

Should the syntax be extended to allow const/&-qualifying individual names’ types?

auto [& x, const y, const& z] = f(); // NOT proposed

We think the answer should be no. This is a simple feature to store a value and bind names to its components, not to declare multiple variables. Allowing such qualification would be feature creep, extending the feature to be something different, namely a way to declare multiple variables. If we do want to declare multiple variables, we already have a way to spell it:

auto val = f(); // or auto&&
T1& x = get<0>(val);
T2 const y = get<1>(val);
T3 const& z = get<2>(val);
like image 71
Vittorio Romeo Avatar answered Nov 05 '22 11:11

Vittorio Romeo