Possible Duplicate:
Can I get a fresh start in C++ without failing again?
Consider T* o = new(T()), where T has a copy constructor defined. Also suppose expression new uses the default ::operator new()
To re-use the memory allocated for o, instead of deleting the object with delete o, does the standard allow the following sequence:
- call
o->~T()explicitly - use placement new for creating a copy of an object on the memory allocated for o previously: new(o) T(x)
- when done with o and its memory, delete it with the
delete o
I also ask this because I don't understand why std::map<T, V> (or its operator[] specifically), for example, requires T to have an appropriate assignment operator defined, if the above sequence could work without this requirement. I doubt that map has been designed this way just because operator=() and the copy constructor can have different semantics, since most of the time they are just implemented the same way.