www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Copying POD structs with 'new'

reply Paul Backus <snarwin gmail.com> writes:
Copying [POD structs][1] 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][2]. It came up again recently in [DMD issue 20950][3], 
regarding placement `new`.

[1]: https://dlang.org/spec/struct.html#POD
[2]: https://issues.dlang.org/show_bug.cgi?id=12918
[3]: https://github.com/dlang/dmd/issues/20950
Mar 14
parent Atila Neves <atila.neves gmail.com> writes:
On Friday, 14 March 2025 at 23:50:53 UTC, Paul Backus wrote:
 Copying [POD structs][1] with `new` is currently harder than it 
 needs to be. It would be easier if the following code were 
 allowed to compile:

 [...]
Makes sense to me.
Mar 24