Given a class Foo which has some value-initializing default constructor:
class Foo {
private:
uint32_t x;
public:
constexpr Foo()
: x { 3 }
{}
// ... and some other constructors
};
I need to allocate an array of these Foo's. I don't want the array's elements' default constructors to run, because later I'm going to initialize each element explicitly anyway. Something like this:
Foo foos[20000];
for (int i = 0; i < 20000; ++i) {
foos[i] = init(i);
}
Is there a way to obtain such an uninitialized array of Foo's given that we're not allowed to change the default constructor of Foo into a non-initializing one?
By the way, this is how you'd create an uninitialized array in D:
Foo[20000] foos = void;
...and here's the same in Rust:
let mut foos: [Foo; 20000] = unsafe { std::mem::uninitialized() };
charmay be used to alias other types, but other type may not be used to aliascharstd::arraydoesn't have constructors.