Reputation: 395
I would like to define class members a
, b
using structured bindings, in something like the following way:
struct AB {
int a;
int b;
};
class MyClass {
public:
MyClass(AB ab) : ab{ab} {}
private:
AB ab;
const auto [a, b] = ab;
}
However, with gcc 9.2.0 this causes the compiler error:
error: expected unqualified-id before ‘[’ token
12 | const auto [a, b] = ab;
Is there some way I can rewrite this with structured bindings so that it compiles? Or must I give up using structured bindings and define each member separately:
const int a = ab.a;
const int b = ab.b;
Upvotes: 1
Views: 143
Reputation: 238361
I would like to define class members ... using structured bindings
Is there some way I can rewrite this with structured bindings so that it compiles?
No. You cannot have structured bindings as members.
Or must I give up using structured bindings and define each member separately:
That would be well-formed.
Regardless of how you declare the members, it is unclear why you'd want to have copies of the members of member as direct members. I recommend reconsidering your design.
Upvotes: 5