( OP ):
, , , , OP ++ 11, ++ 11.
Foo, :
union FooStorage {
Foo data;
FooStorage() {
}
template <typename... Args>
void init(Args&&... args) {
new (&data) Foo{static_cast<Args&&>(args)...};
}
~FooStorage() {
data.~Foo();
}
};
, data, . data ( init()) . ( OP , , .)
, { ... } ( ... ) . Foo.
10 Foo
FooStorage buffer[10];
i -th Foo:
buffer[i].init();
To use i-th Foo, for example, by calling its method do_something:
buffer[i].data.do_something();
This is the main idea. There are many improvements that can be made with FooStorage.
source
share