Does placement-new introduce a sequence point?
Asked Answered
A

5

12

Consider the following line of code:

new (p++) T();

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

Akkadian answered 27/6, 2011 at 17:56 Comment(2)
It's a nice question, but I hope that you would never actually write code like this :-)Unbridled
@sel: Indeed, I replaced it with new (p) T(); ++p; because it is easier to understand and the differing semantics (no increment when exception is thrown) fit my program logic better.Akkadian
F
8

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.

Farl answered 27/6, 2011 at 18:16 Comment(0)
S
4

Yes it is guaranteed to be incremented.

The operators are just syntactic sugar for function/method calls.
I don't believe new has any special meaning above operator so it should be the same.

Thus all parameter are fully evaluated (with sequence point) before the function new is called.

Shackleford answered 27/6, 2011 at 18:15 Comment(0)
H
3

I don't think the standard answers this question directly/explicitly. Implicitly, however, the answer is yes.

In particular, the placement syntax for new is simply a way of specifying extra parameters that will be passed to a function. Like any other function call, there is a sequence point between evaluating all the parameters to the function (in unspecified order), and executing any code in the function. I believe that should mean your p++ will be evaluated and all side effects applied before anything else happens.

Heilner answered 27/6, 2011 at 18:15 Comment(0)
T
0

Placement new is just a regular function, named operator new(size_t, void*). It simply returns its second argument.

Theroid answered 27/6, 2011 at 18:14 Comment(3)
You are confusing placement-new with operator new.Akkadian
Don't think so. Well, to be precize, placement-new is a syntax of calling overloaded forms of operator-new, but normal oeople just call these forms placement-new too.Theroid
Placement new and operator new are orthogonal concepts. Very orthogonal concepts. If you use placement new there is no call to operator new.Seismograph
A
-1

The increment operator does the following:

  1. stores the old value in a internal copy
  2. increments by one
  3. returns the old value
  4. deletes the copy of the old value

So, p is guaranteed to be incremented.

Accidie answered 27/6, 2011 at 18:7 Comment(3)
Don't be so sure. For example, see this question: #6482535Oxytetracycline
The increment is not guaranteed to be visible until the next sequence point, and that is what my question is about.Akkadian
Ok @FredOverflow, missed the topic in the answer, sry @Fred Larson, your are right, i should tell: This is only the standard definition. If a compiler does it not this way, thats something else.Accidie

© 2022 - 2024 — McMap. All rights reserved.