Copying POD structs with new
is currently harder than it needs to be. It would be easier if the following code were allowed to compile:
struct Example { int x, y; }
Example original = { 123, 456 };
Example* copyPtr = new Example(original); // copies original onto the heap
Currently, this code fails to compile, because Example
does not have a copy constructor. The proposed behavior is that, because Example
is a POD struct, it should not require a copy constructor—the compiler can simply generate code that performs a bit copy.
This was originally requested in 2014 in Bugzilla issue 12918. It came up again recently in DMD issue 20950, regarding placement new
.