Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of structs can structured bindings work with

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?

like image 487
Curious Avatar asked Dec 01 '16 02:12

Curious


People also ask

How do structured bindings work?

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.

What is a structure bound?

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.


1 Answers

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 of E or of the same unambiguous public base class of E, 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 of E.

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.

like image 178
Kerrek SB Avatar answered Oct 04 '22 01:10

Kerrek SB