top | item 12018291

(no title)

squidbidness | 9 years ago

To clarify a question raised in this write-up:

"auto [a , b , c] = getvalues();

"The braces are needed, getvalues returns a tuple. std::pair is not mentioned in the proposal, so its unclear if this works with pair, which is returned by the STL in some insert methods."

Destructured bindings will work with arrays; plain aggregate structs; and any class that offers a specialization of std::get<>(), so both std::tuple and std::pair are supported as a consequence. (C++11 added std::get<>() for std::pair).

discuss

order

ant6n|9 years ago

What if the number of lhs values don't match the number of rhs values?

Kristine1975|9 years ago

Compile-time error:

Otherwise, if the expression std::tuple_size<E>::value is a well-formed integral constant expression, the number of elements in the identifier-list shall be equal to the value of that expression

(E is the return type of "getvalues" in squidbidness's example, the initializer-list is "a, b, c".)