Reputation: 6646
Basically, if I have a variable T value
, and a function T func(const T &)
that transforms a T
into another, can I do new (&value) T(func(value))
? I'm not sure if rewriting the same place can cause problem. Can the old value
get overwritten before func(value)
finishes?
UPDATE: I'm doing this to try to turn a tail-recursion into a loop (compilers can't optimize it due to non-trivial destructor). Methods better than placement new are more than welcome!
Upvotes: 2
Views: 323
Reputation: 75844
No you cannot do that (safely). Placement new must be used on an allocated but uninitialized memory or on a POD object.
You can't just bypass constructors, copy, move ctors/assignments and destructors on objects (except for PODs). If you want to do that, there is something wrong on your design and you need to go to the drawing board.
Upvotes: 2