Search code examples
c++exceptionconstructorside-effectssequence-points

Does placement-new introduce a sequence point?


Consider the following line of code:

new (p++) T();

If the constructor T() throws an exception, is p guaranteed to have already been incremented?


Solution

  • From 5.3.4 [expr.new] (quoting from n3242):

    11 The new-placement syntax is used to supply additional arguments to an allocation function. If used, overload resolution is performed on a function call created by assembling an argument list consisting of the amount of space requested (the first argument) and the expressions in the new-placement part of the new-expression (the second and succeeding arguments).

    So in a new expression the allocation function is used from a function call (which makes sense). All allocation functions are functions, including the ones provided by the implementation, from 3.7.4.1 [basic.stc.dynamic.allocation]:

    1 An allocation function shall be a class member function or a global function; [...]

    So by the time an exception is thrown from the constructor, the allocation has taken place and the associated function call expression has been fully evaluated.