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
?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With