public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Guaranteed copy elision
@ 2022-11-15  5:47 Yubin Ruan
  2022-11-18 15:05 ` Stefan Ring
  2022-11-19 10:12 ` Jonathan Wakely
  0 siblings, 2 replies; 8+ messages in thread
From: Yubin Ruan @ 2022-11-15  5:47 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1247 bytes --]

Hi,

As mentioned in cppreference:
https://en.cppreference.com/w/cpp/language/copy_elision

it is guaranteed in c++17 that copy elision must be applied for some cases
like

    SomeBigObject SomeBigObject::Factory(...) {
        SomeBigObject local;
         ...
         return local;
    }

(examples taken from https://abseil.io/tips/11 )

but not for cases like

    SomeBigObject SomeBigObject::Factory(...) {
        SomeBigObject local1;
        SomeBigObject local2;
         ...

         if (cond_1) {
             return local1;
         } else {
             return local2;
         }
    }

For a c++ user, it is somewhat difficult to be 100% sure that copy elision
/ NVO is applied to the functions' return value above.

To be sure that a object would not be copied, we usually write something
like

         SomeBigObject obj;
         func(&obj);

while in most of the cases a one-liner like

         SomeBigObject obj = func();

would suffice.

Is there any language facility to help us guarantee that at compile time
(such as some kind of static_assert() ) so that we can be confident writing
those one-liner ?

I know that marking the copy constructor deleted would do the good, but
copy is needed in some cases.

Thanks
Yubin

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-11-21 14:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-15  5:47 Guaranteed copy elision Yubin Ruan
2022-11-18 15:05 ` Stefan Ring
2022-11-18 21:35   ` Martin Oberzalek
2022-11-18 21:58     ` Segher Boessenkool
2022-11-19 10:07       ` Jonathan Wakely
2022-11-19 10:12 ` Jonathan Wakely
2022-11-21 14:17   ` Yubin Ruan
2022-11-21 14:20     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).