new-expression evaluation order in C++17

Viewed 104

By cppreference/eval_order rule 12 the call to the allocation function (operator new) is sequenced before (since C++17) the evaluation of the constructor arguments in a new-expression. Thus, I thought the following

Bar b;
Foo* f = new Foo(b.fun());

at runtime results in calling new first then fun() on b and lastly the constructor of Foo. Both Clang and ICC produce this order. However GCC calls fun() first, then the operator new and then the constructor.

Who is right? What am I missing?

1 Answers

Looking into the standard (version N4835.pdf):

7.6.2.7 New [expr.new]
23 The invocation of the allocation function is sequenced before the evaluations of expressions in the new-initializer. Initialization of the allocated object is sequenced before the value computation of the new-expression.

In your case b.fun() is the only expression in the new-initializer.

It looks like Clang, ICC and MSVC do it right and GCC does it wrong. It could be worth asking the GCC guys and maybe filing a bug report (or maybe there already exists one?).

Related