public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Yubin Ruan <ablacktshirt@gmail.com>
To: Jonathan Wakely <jwakely.gcc@gmail.com>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Guaranteed copy elision
Date: Mon, 21 Nov 2022 22:17:45 +0800	[thread overview]
Message-ID: <CAJYFCiOtHWYRKMGtMka=vidM+jbq03nURXyuPQ4VNAw-vhPH1Q@mail.gmail.com> (raw)
In-Reply-To: <CAH6eHdQ+Xsxp=msP9HLr7t+BiN1jkvkNE5NThbaC_mTMWuL_nQ@mail.gmail.com>

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

On Sat, Nov 19, 2022 at 6:12 PM Jonathan Wakely <jwakely.gcc@gmail.com>
wrote:

>
>
> On Tue, 15 Nov 2022, 05:48 Yubin Ruan via Gcc-help, <gcc-help@gcc.gnu.org>
> wrote:
>
>> 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;
>>     }
>>
>
> No, this is not guaranteed. As the cppreference page explains, it's only
> guaranteed for copying temporaries. This is not a temporary.
>
> Elision here is allowed, but not required. It's commonly implemented
> though.
>
>
>
>
>> (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 ?
>>
>
> Make it cheap to move, and then it will be moved instead of copied. The
> move isn't always elided, but if it's cheap then the code will still be
> efficient.
>

Thank you all.

It seems that currently the only way to guarantee  efficiency is to use
move instead.

--
Yubin

  reply	other threads:[~2022-11-21 14:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-15  5:47 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 [this message]
2022-11-21 14:20     ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJYFCiOtHWYRKMGtMka=vidM+jbq03nURXyuPQ4VNAw-vhPH1Q@mail.gmail.com' \
    --to=ablacktshirt@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).