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

--- Comment #2 from Richard Smith <richard-gccbugzilla at metafoo dot co.uk> ---
> Where does the scope of object A inside Foo begin and end?

There is formally only one object. The object constructed in the return
statement of Foo is simply the same object that is named 'a' within Bar.


> Maybe it is time to file a DR to the C++ language folks about this case
> if there is not one already.

Hmm, the [class.temporary]/3 rule already gets very close to allowing this:

"When an object of class type X is passed to or returned from a function, if
each copy constructor, move constructor, and destructor of X is either trivial
or deleted, and X has at least one non-deleted copy or move constructor,
implementations are permitted to create a temporary object to hold the function
parameter or result object. The temporary object is constructed from the
function argument or return value, respectively, and the function’s parameter
or return object is initialized as if by using the non-deleted trivial
constructor to copy the temporary (even if that constructor is inaccessible or
would not be selected by overload resolution to perform a copy or move of the
object). [ Note: This latitude is granted to allow objects of class type to be
passed to or returned from functions in registers. — end note ]"

In fact, I think the *only* problem here is that the above rule does not allow
the caller and the callee to observe the objects having the same address.

I'll take this to the committee and see whether we can get that rule relaxed
slightly.

Would you be OK with this optimization only being permitted for the above class
types (those that can be passed/returned in registers)?

Reply via email to