Discussion:
g++ 14 new feature bug
(too old to reply)
Bonita Montero
2024-10-06 16:22:15 UTC
Permalink
g++ 14 has a new feature which that warns when a return value
optimization / copy elision doesn't take place. Here's my example:

https://godbolt.org/z/G8Eh1hrsd

As you can see the compiler warns twice - but moves according to
the program output nevertheless. Is this a bug ?
Bonita Montero
2024-10-06 16:52:19 UTC
Permalink
Post by Bonita Montero
g++ 14 has a new feature which that warns when a return value
https://godbolt.org/z/G8Eh1hrsd
As you can see the compiler warns twice - but moves according to
the program output nevertheless. Is this a bug ?
Sorry, misconception: copy elision doesn't mean that a copy is
transformed into a move but that the copy / move does take place
in the callers place.
Andrey Tarasevich
2024-10-06 20:00:33 UTC
Permalink
Post by Bonita Montero
g++ 14 has a new feature which that warns when a return value
https://godbolt.org/z/G8Eh1hrsd
As you can see the compiler warns twice - but moves according to
the program output nevertheless. Is this a bug ?
NRVO (which is what the option name refers to) is not about moving
instead of copying. It is about eliminating the named local object
entirely, effectively transforming it into a reference to the
caller-owned recipient object. In case of NRVO the result is constructed
directly in the external recipient object.

In your case the NRVO does not happen. Hence the warning.
--
Best regards,
Andrey
Bonita Montero
2024-10-07 08:47:59 UTC
Permalink
Post by Andrey Tarasevich
Post by Bonita Montero
g++ 14 has a new feature which that warns when a return value
https://godbolt.org/z/G8Eh1hrsd
As you can see the compiler warns twice - but moves according to
the program output nevertheless. Is this a bug ?
NRVO (which is what the option name refers to) is not about moving
instead of copying. It is about eliminating the named local object
entirely, effectively transforming it into a reference to the caller-
owned recipient object. In case of NRVO the result is constructed
directly in the external recipient object.
In your case the NRVO does not happen. Hence the warning.
I already corrected myself in that sense before you posted.

Loading...