Curious
Curious

Reputation: 21510

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?

Upvotes: 8

Views: 900

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476950

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.

Upvotes: 5

Related Questions