Reputation: 922
I would like to create a class with several const
member variables. Unfortunately, the function I need to initialize those variables is from a very C-style external library. As a result, I have been employing the following "middle-man" of sorts:
struct Struct {
int foo, bar;
Struct(int baz, int qux) {
initializerFunction(&foo, baz, &bar, qux);
}
}
class Class {
const int foo, bar;
Class(Struct s) : anArray(s.foo), anotherArray(s.bar) {}
public:
Class(int baz, int quz) : Class(Struct(baz, qux)) {}
}
However, I somewhat dislike this construction. I was hoping to be able to do something like this instead:
class Class {
const int foo, bar;
public:
Class(int baz, int qux) : [foo, bar](Struct(baz, qux)) {}
}
Is it possible that I am just using incorrect syntax, or is structured binding truly not allowed in the member initializer list of a constructor?
Upvotes: 4
Views: 684
Reputation: 473577
Structured bindings declare variables (well, they're not "variables" per-se, but nevermind). You cannot use structured bindings to manipulate existing objects. This includes member objects.
Upvotes: 6