https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68103

            Bug ID: 68103
           Summary: Unnecessary copying due to order of evaluation with
                    operator new
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: glisse at gcc dot gnu.org
  Target Milestone: ---

#include <new>
struct A{A(A const&);};
A f();
void g(A*p){new(p)A(f());}

(the same applies to other variants of new, so DR 1748 (is there a PR for that
one?) will not fix it)

g++ calls f before operator new. If the result of operator new is non-zero, it
then copies the result to *p.

On the other hand, clang calls operator new first. It then tests for zero, and
if not it calls f, letting it use *p directly for its return value.

The order of evaluation chosen by gcc seems to prevent this copy elision, for a
benefit that is unlikely to be comparable, so I would be in favor of changing
it in this particular case (both orders are legal).

(I just noticed that a possibly related paper by Gaby was discussed last week,
I didn't check if it says anything about this particular case)

Reply via email to