Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.iso-c++ > #1995

Re: Move semantics und operator overloading

From ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups de.comp.lang.iso-c++
Subject Re: Move semantics und operator overloading
Date 2017-01-08 22:26 +0000
Organization Stefan Ram
Message-ID <move-20170108232127@ram.dialup.fu-berlin.de> (permalink)
References <0b8ad143-ccf2-4867-a02b-ed795dfb392c@googlegroups.com> <move-20170107173529@ram.dialup.fu-berlin.de> <99988bba-1453-4e4a-bf29-a11886a5851e@googlegroups.com> <RVO-20170108145851@ram.dialup.fu-berlin.de> <30d04616-3615-44c7-a4aa-9f6a50d3b8af@googlegroups.com>

Show all headers | View raw


Helmut Zeisel <zei2011@liwest.at> writes:
>Ich weiß jetzt nicht genau, was Du mit "Deswegen ist es
>besser, wenn es bei der Rückgabe kopiert wird." meinst. Bei
>der Rückgabe wird jedenfalls nichts kopiert, sondern statt
>einer Kopie die RVO angewendet.

  Ich habe hier mal ein Programm geschrieben, das zeigt, daß
  ein konstanter Referenzparameter in solchen Fällen bei der
  Rückgabe in das Ziel kopiert wird.

  Er kann ja schon deshalb nicht verschoben werden, weil es
  ein /konstanter/ Referenzparameter ist.

  Quelltext

#include <initializer_list>
#include <iostream>
#include <string>

static void escape( void * p ) 
{ asm volatile( "" : : "g"(p) : "memory" ); }

static int i = 0;

struct object
{ object() = default;
  object( const object & )
  { ::std::cout <<
    ( i++ ? "Copying r into o2" : 
      "Copying o into c" )<< '\n'; }
  object( object && ) noexcept
  { ::std::cout << "Moving c into o1" << '\n'; }};

static object f( object c )
{ ::std::cout << "in f\n"; return c; }

static object g( object const & r )
{ ::std::cout << "in g\n"; return r; }

int main()
{ object o;
  ::std::cout << '\n' << "before f" << '\n';
  object o1 = f( o ); escape( &o1 );
  ::std::cout << '\n' << "before g" << '\n';
  object o2 = g( o ); escape( &o2 );
  ::std::cout << '\n'; }

  Ausgabe

before f
Copying o into c
in f
Moving c into o1

before g
in g
Copying r into o2

  Neben der RVO kann übrigens auch noch die As-If-Regel
  einer Implementation zusätzliche Möglichkeiten eröffnen.

  Im folgenden Program zeige ich, was passiert wenn wir 
  das Kopieren mutwillig mit einem »::std::move« unterbinden.

#include <initializer_list>
#include <iostream>
#include <string>

static void escape( void * p ) 
{ asm volatile( "" : : "g"(p) : "memory" ); }

struct object
{ char const * state = "good state";
  object() = default;
  object( object const & )= default;
  object( object && other ) noexcept
  { this->state = other.state;
    other.state = "valid state with an unspecified value"; }};

static object f( object & r )
{ return ::std::move( r ); }

int main()
{ object o;
  ::std::cout << "o has a " << o.state << ".\n";
  object o1 = f( o ); escape( &o1 );
  ::std::cout << "o has a " << o.state << ".\n"; }

  Ausgabe

o has a good state.
o has a valid state with an unspecified value.

  In diesem Fall wurde nun das Objekt »o« durch
  den Aufruf »f( o )« in einen unspezifizierten
  Zustand versetzt. Wenn man »o« lediglich als
  Summand einer Summe verwenden will, sollte 
  dies aber nicht passieren.

Back to de.comp.lang.iso-c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-06 23:53 -0800
  Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-07 15:38 +0000
  Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-07 16:36 +0000
    Re: Move semantics und operator overloading Helmut Zeisel <helmut.zeisel@gmail.com> - 2017-01-07 12:10 -0800
      Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-08 14:00 +0000
        Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-08 07:27 -0800
          Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-08 22:26 +0000
            Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-09 00:50 -0800
              Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-09 20:26 +0000
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-09 22:59 -0800
                Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-10 17:32 +0000
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-10 09:56 -0800
                Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-11 00:21 +0000
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-10 23:41 -0800
                Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-11 10:04 +0000
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-11 02:33 -0800
                Re: Move semantics und operator overloading Bonita Montero <Bonita.Montero@gmail.com> - 2017-01-11 15:43 +0100
                Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-11 22:27 +0000
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-11 22:22 -0800
                Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-12 18:15 +0000
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-12 12:34 -0800
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-13 03:30 -0800
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-11 23:53 -0800
                Re: Move semantics und operator overloading ram@zedat.fu-berlin.de (Stefan Ram) - 2017-01-12 20:23 +0000
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-13 00:47 -0800
                Re: Move semantics und operator overloading Helmut Zeisel <zei2011@liwest.at> - 2017-01-11 00:15 -0800

csiph-web