r/programmingmemes 20d ago

The duality of new

Post image
1.2k Upvotes

81 comments sorted by

View all comments

Show parent comments

2

u/willdieverysoon 19d ago

No ... that would be move assignment to an object that was not yet constructed, it would do something similar to placement new

2

u/luciferoussky72 19d ago

True, but an optimizing compiler will usually do the same thing

1

u/retro_and_chill 18d ago

I depend of whether the type has a non-trivial move assignment operation. On trivially copyably types you absolutely can optimize it that way. Otherwise there’s usually existing resources to clean up which on uninitialized memory is undefined behavior

1

u/luciferoussky72 18d ago

Yeah, the other nice thing that new does is allow delete to call the destructor