TIL: In C++, placement new (new (p) Foo(), where p is void*) is - TopicsExpress



          

TIL: In C++, placement new (new (p) Foo(), where p is void*) is required by the standard to check if p is null, and do nothing if it is. Gory details: p is actually an argument to an allocation function: void* operator new(size_t, void*) which is declared noexcept in header (per 18.6) 5.3.4.13: "If the allocation function returns null, initialization shall not be done, the deallocation function shall not be called, and the value of the new-expression shall be null." The rationale is that an allocation function can signal an error by throwing std::bad_alloc, or, if declared as non-throwing (noexcept, throw()), by returning 0, and placement new should work with either.
Posted on: Thu, 29 Aug 2013 00:15:40 +0000

Recently Viewed Topics




© 2015