Is new T() equivalent to `mem = operator new(sizeof(T)); new(mem)T`?

This does appear to be valid, as long as T is not an array type, and is not a class with a custom operator delete visible from its scope which is incompatible with the operator new you invoke.

[expr.delete]/2 says the operand of delete may be "a pointer to a non-array object created by a previous new-expression". The new-expression grammar syntax symbol does include the placement new syntax, and [expr.delete] doesn't say the previous new-expression needs to be a non-placement variety. And any new-expression including a placement new is considered to "create" an object.

The operator new allocation function and operator delete deallocation function involved do need to match up. operator new(sizeof(T)) will normally call the global ordinary allocation function, but to be more sure you can write ::operator new(sizeof(T)). The deallocation function is looked up in the scope of class T, so a static operator delete(void*); in a class could break this.

You might want to consider exception safety, though. A straight T* p = ::new T; is actually more equivalent to:

void* mem = ::operator new(sizeof(T));
T* p;
try {
    p = ::new(mem) T;
} catch (...) {
    ::operator delete(mem, sizeof(T));
    throw;
}

Tags:

C++