public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RVO (let the compiler copy)
@ 2013-12-19  8:31 Henrik Mannerström
  2013-12-19 10:41 ` Marc Glisse
  0 siblings, 1 reply; 3+ messages in thread
From: Henrik Mannerström @ 2013-12-19  8:31 UTC (permalink / raw)
  To: gcc-help

This question came up on SO (
http://stackoverflow.com/questions/7320520/optimizing-the-number-of-constructor-calls)
and I was baffled since it went against the mantra "let the compiler do
the copying". Do you have an idea of what is going on? Why is the
commented version of operator+ worse? I compile with g++ -std=gnu++11
-Wall -Wextra -pedantic -O2  mwe.cc

BR,
Henrik

#include <iostream>

struct Imaginary {
  Imaginary(int a_, int b_) : a(a_), b(b_) {
    std::cout << "int/int ctor" << std::endl;
  };
 
  Imaginary(Imaginary const & orig) {
    a = orig.a;
    b = orig.b;
    std::cout << "Copy ctor (" << a << ',' << b << ')' << std::endl;
  }

  Imaginary & append(Imaginary const & rhs) {
    a = 10 * a + rhs.a;
    b = 10 * b + rhs.b;
    return *this;
  }

  int a;
  int b;
};

/* This calls the copy constructor once */
Imaginary operator+(Imaginary const & lhs, Imaginary const & rhs) {
  Imaginary tmp(lhs);
  tmp.append(rhs);
  return tmp;
}

/* This calls the copy constructor twice
Imaginary operator+(Imaginary lhs, Imaginary const & rhs) {
  lhs.append(rhs);
  return lhs;
}
*/

int main()
{
    Imaginary x(1, 1);
    Imaginary y(2, 1);

    Imaginary c = x + y;
    return c.a;
}

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

* Re: RVO (let the compiler copy)
  2013-12-19  8:31 RVO (let the compiler copy) Henrik Mannerström
@ 2013-12-19 10:41 ` Marc Glisse
  2013-12-19 10:50   ` Henrik Mannerström
  0 siblings, 1 reply; 3+ messages in thread
From: Marc Glisse @ 2013-12-19 10:41 UTC (permalink / raw)
  To: Henrik Mannerström; +Cc: gcc-help

On Thu, 19 Dec 2013, Henrik Mannerström wrote:

> This question came up on SO (
> http://stackoverflow.com/questions/7320520/optimizing-the-number-of-constructor-calls)
> and I was baffled since it went against the mantra "let the compiler do
> the copying". Do you have an idea of what is going on? Why is the
> commented version of operator+ worse?

See:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57176

-- 
Marc Glisse

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

* Re: RVO (let the compiler copy)
  2013-12-19 10:41 ` Marc Glisse
@ 2013-12-19 10:50   ` Henrik Mannerström
  0 siblings, 0 replies; 3+ messages in thread
From: Henrik Mannerström @ 2013-12-19 10:50 UTC (permalink / raw)
  To: gcc-help

On 12/19/2013 12:41 PM, Marc Glisse wrote:
> On Thu, 19 Dec 2013, Henrik Mannerström wrote:
>
>> This question came up on SO (
>> http://stackoverflow.com/questions/7320520/optimizing-the-number-of-constructor-calls)
>>
>> and I was baffled since it went against the mantra "let the compiler do
>> the copying". Do you have an idea of what is going on? Why is the
>> commented version of operator+ worse?
>
> See:
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57176
>
Thank you!

I was completely blinded by "pass by value" propaganda
(http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/). Now I
won't be so naive.

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

end of thread, other threads:[~2013-12-19 10:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-19  8:31 RVO (let the compiler copy) Henrik Mannerström
2013-12-19 10:41 ` Marc Glisse
2013-12-19 10:50   ` Henrik Mannerström

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).