public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Oberzalek <kingleo@gmx.at>
To: gcc-help@gcc.gnu.org
Subject: Re: Guaranteed copy elision
Date: Fri, 18 Nov 2022 22:35:20 +0100	[thread overview]
Message-ID: <32119174b061d5bf07e3d39063709f852ff5ca5f.camel@gmx.at> (raw)
In-Reply-To: <CAAxjCEybwpBm05QtUEHPpfzfJ39mvqiXWRGAC164Ggw=X9o41w@mail.gmail.com>

Am Freitag, dem 18.11.2022 um 16:05 +0100 schrieb Stefan Ring via Gcc-
help:
> On Tue, Nov 15, 2022 at 6:48 AM Yubin Ruan via Gcc-help
> <gcc-help@gcc.gnu.org> wrote:
> > 
> > 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 ?
> 
> Interesting question, but unfortunately I do not have a good answer!
> I
> can only bump the thread. ;)

With c++ language features this may is a solution:


#include <iostream>
#include <vector>

class DoNotCopy
{

  std::vector<std::string> myData;
  
public:
  DoNotCopy( const DoNotCopy & other ) = delete;
  DoNotCopy & operator=( const DoNotCopy & other ) = delete;

  // valid move semantic
  // data within the vector will be moved, not copied
  DoNotCopy( const DoNotCopy && other )
    : myData( other.myData )
  {}

  DoNotCopy()
    : myData()
  {
  }
  
};


DoNotCopy func()
{
  DoNotCopy dnc;

  return dnc;
}

int main()
{
  DoNotCopy dnc = func(); // works

  DoNotCopy dnc2 = dnc; // error

  DoNotCopy dnc3;
  dnc3 = dnc; // error
}


  reply	other threads:[~2022-11-18 21:35 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 [this message]
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

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=32119174b061d5bf07e3d39063709f852ff5ca5f.camel@gmx.at \
    --to=kingleo@gmx.at \
    --cc=gcc-help@gcc.gnu.org \
    /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).