Binds the specified names to subobjects or elements of the initializer.
Like a reference, a structured binding is an alias to an existing object. Unlike a reference, the type of a structured binding does not have to be a reference type.
attr(optional) cv-auto ref-operator(optional) [ identifier-list ] = expression ; | (1) | |
attr(optional) cv-auto ref-operator(optional) [ identifier-list ] { expression } ; | (2) | |
attr(optional) cv-auto ref-operator(optional) [ identifier-list ] ( expression ) ; | (3) |
attr | - | sequence of any number of attributes |
cv-auto | - | possibly cv-qualified type specifier auto |
ref-operator | - | either & or && |
identifier-list | - | list of comma-separated identifiers introduced by this declaration |
expression | - | an expression that does not have the comma operator at the top level (grammatically, an assignment-expression), and has either array or non-union class type. If expression refers to any of the names from identifier-list, the declaration is ill-formed. |
A structured binding declaration introduces all identifiers in the identifier-list as names in the surrounding scope and binds them to subobjects or elements of the object denoted by expression. The bindings so introduced are called structured bindings.
A structured binding declaration first introduces a uniquely-named variable (here denoted by e
) to hold the value of the initializer, as follows:
A
and no ref-operator is present, then e
has type cv A
, where cv is the cv-qualifiers in the cv-auto sequence, and each element of e
is copy- (for (1)) or direct- (for (2,3)) initialized from the corresponding element of expression. e
is defined as if by using its name instead of [
identifier-list ]
in the declaration. We use E
to denote the type of the expression e
. (In other words, E
is the equivalent of std::remove_reference_t<decltype((e))>
.).
A structured binding declaration then performs the binding in one of three possible ways, depending on E
:
E
is an array type, then the names are bound to the array elements. E
is a non-union class type and std::tuple_size<E>
is a complete type, then the "tuple-like" binding protocol is used. E
is a non-union class type but std::tuple_size<E>
is not a complete type, then the names are bound to the accessible data members of E
. Each of the three cases is described in more detail below.
Each structured binding has a referenced type, defined in the description below. This type is the type returned by decltype
when applied to an unparenthesized structured binding.
Each identifier in the identifier-list becomes the name of an lvalue that refers to the corresponding element of the array. The number of identifiers must equal the number of array elements.
The referenced type for each identifier is the array element type. Note that if the array type E
is cv-qualified, so is its element type.
int a[2] = {1,2}; auto [x,y] = a; // creates e[2], copies a into e, then x refers to e[0], y refers to e[1] auto& [xr, yr] = a; // xr refers to a[0], yr refers to a[1]
The expression std::tuple_size<E>::value
must be a well-formed integer constant expression, and the number of identifiers must equal std::tuple_size<E>::value
.
For each identifier, a variable whose type is "reference to std::tuple_element<i, E>::type
" is introduced: lvalue reference if its corresponding initializer is an lvalue, rvalue reference otherwise. The initializer for the i-th variable is.
e.get<i>()
(where i
is a std::size_t prvalue), if lookup for the identifier get
in the scope of E
by class member access lookup finds at least one declaration that is a function template whose first template parameter is a non-type parameter get<i>(e)
, where get
is looked up by argument-dependent lookup only, ignoring non-ADL lookup. In these initializer expressions, e
is an lvalue if the type of the entity e
is an lvalue reference (this only happens if the ref-operator is &
or if it is &&
and the initializer expression is an lvalue) and an xvalue otherwise (this effectively performs a kind of perfect forwarding), and <i>
is always interpreted as a template parameter list.
The identifier then becomes the name of an lvalue that refers to the object bound to said variable.
The referenced type for the i-th identifier is std::tuple_element<i, E>::type
.
float x{}; char y{}; int z{}; std::tuple<float&,char&&,int> tpl(x,std::move(y),z); const auto& [a,b,c] = tpl; // a names a structured binding that refers to x; decltype(a) is float& // b names a structured binding that refers to y; decltype(b) is char&& // c names a structured binding that refers to the 3rd element of tpl; decltype(c) is const int
Every non-static data member of E
must be a direct member of E
or the same base class of E
, and must be well-formed in the context of the structured binding when named as e.name
. E
may not have an anonymous union member. The number of identifiers must equal the number of non-static data members.
Each identifier in identifier-list becomes the name of an lvalue that refers to the next member of e
in declaration order (bit fields are supported); the type of the lvalue is cv T_i
, where cv
is the cv-qualifiers of E
and T_i
is the declared type of the i-th member.
The referenced type of the i-th identifier is cv T_i
.
struct S { int x1 : 2; volatile double y1; }; S f(); const auto [x, y] = f(); // x is a const int lvalue identifying the 2-bit bit field // y is a const volatile double lvalue
The lookup for member get
ignores accessibility as usual and also ignores the exact type of the non-type template parameter. A private template<char*> void get();
member will cause the member interpretation to be used, even though it is ill-formed.
The portion of the declaration preceding [
applies to the hidden variable e
, not to the introduced identifiers.
int a = 1, b = 2; const auto& [x, y] = std::tie(a, b); // x and y are of type int& auto [z, w] = std::tie(a, b); // z and w are still of type int& assert(&z == &a); // passes
The tuple-like interpretation is always used if std::tuple_size<E>
is a complete type, even if that would cause the program to be ill-formed:
struct A { int x; }; namespace std { template<> struct tuple_size<::A> {}; } auto [x] = A{}; // error; the "data member" interpretation is not considered.
The usual rules for reference-binding to temporaries (including lifetime-extension) apply if a ref-operator is present and the expression is a prvalue. In those cases the hidden variable e
is a reference that binds to the temporary variable materialized from the prvalue expression, extending its lifetime. As usual, the binding will fail if e
is a non-const lvalue reference:
int a = 1; const auto& [x] = std::make_tuple(a); // OK, not dangling auto& [y] = std::make_tuple(a); // error, cannot bind auto& to rvalue std::tuple auto&& [z] = std::make_tuple(a); // also OK
decltype(x)
, where x
denotes a structured binding, names the referenced type of that structured binding. In the tuple-like case, this is the type returned by std::tuple_element
, which may not be a reference even though the structured binding itself in fact always behaves like a reference in this case. This effectively emulates the behavior of binding to a struct whose non-static data members have the types returned by tuple_element
, with the referenceness of the binding itself being a mere implementation detail.
std::tuple<int, int&> f(); auto [x, y] = f(); // decltype(x) is int // decltype(y) is int& const auto [z, w] = f(); // decltype(z) is const int // decltype(w) is int&
#include <set> #include <string> #include <iomanip> #include <iostream> int main() { std::set<std::string> myset; if (auto [iter, success] = myset.insert("Hello"); success) std::cout << "insert is successful. The value is " << std::quoted(*iter) << '\n'; else std::cout << "The value " << std::quoted(*iter) << " already exists in the set\n"; }
Output:
insert is successful. The value is "Hello"
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P0961R1 | C++17 | in the tuple-like case, member get is used if lookup finds a get of whatever kind | only if lookup finds a function template with a non-type parameter |
P0969R0 | C++17 | in the binding-to-members case, the members are required to be public | only required to be accessible in the context of the declaration |
creates a tuple of lvalue references or unpacks a tuple into individual objects (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/language/structured_binding