I skimmed through the paper on structured bindings here http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0144r0.pdf but I was not able to get a good sense of which types of structs the syntax allows binding to. My best guess is that the struct has to be an aggregate type. Or something with only public data members.
Is there any caveat I am missing to this?
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.
Structured binding is one of the newest features of C++17 that binds the specified names to subobjects or elements of initializer. In simple words, Structured Bindings give us the ability to declare multiple variables initialized from a tuple or struct.
If you don't want to specialize std::tuple_size
, std::tuple_element
and get
for your type, then [dcl.decomp] requires:
Otherwise, all of
E
’s non-static data members shall be public direct members ofE
or of the same unambiguous public base class ofE
,E
shall not have an anonymous union member, and the number of elements in the identifier-list shall be equal to the number of non-static data members ofE
.
So essentially all data members need to be declared in the same class, and they all need to be public, and you need to provide the same number of names as there are members.
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