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


Groups > comp.lang.c++ > #87126

Is Copy-and-swap idiom too slow in assignment operator?

From JiiPee <kerrttuPoistaTama11@gmail.com>
Newsgroups comp.lang.c++
Subject Is Copy-and-swap idiom too slow in assignment operator?
Date 2022-10-22 09:55 +0300
Organization A noiseless patient Spider
Message-ID <tj0448$rtfp$3@dont-email.me> (permalink)

Show all headers | View raw


In a class when defining an assignment operator:

operator=(const Obj& other)

, if one uses the Copy-and-swap idiom to copy @other to this:

Obj copy(other);
copy.swap(*this);

, this is obviously really good looking and elegant etc. But just 
wondering how much slower it would be than a straight/old (not so 
elegant/risky):

this->a = other.a;
this->b = other.b;
...

? I was just checking, if doing swap(), then needs to make 2 or 3 times 
more copy operations in total complare to old way for simple data types 
(int, float, double, bool...). If one has many of these, then is it 
gonna be much slower?

Or... is the compiler gonna optimize out these extra integer/double copy 
operations?

Back to comp.lang.c++ | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Is Copy-and-swap idiom too slow in assignment operator? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-10-22 09:55 +0300
  Re: Is Copy-and-swap idiom too slow in assignment operator? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-10-22 02:16 -0700
  Re: Is Copy-and-swap idiom too slow in assignment         operator? Sam <sam@email-scan.com> - 2022-10-22 07:50 -0400
    Re: Is Copy-and-swap idiom too slow in assignment operator? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-10-22 16:02 +0300
  Re: Is Copy-and-swap idiom too slow in assignment operator? Juha Nieminen <nospam@thanks.invalid> - 2022-10-24 07:10 +0000
    Re: Is Copy-and-swap idiom too slow in assignment operator? JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-10-24 17:38 +0300
      Re: Is Copy-and-swap idiom too slow in assignment operator? Öö Tiib <ootiib@hot.ee> - 2022-10-29 11:59 -0700

csiph-web