hmm.... two things to clarify...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • grahamo

    hmm.... two things to clarify...

    Hi,

    I have something that I'd like to clarify here.... there are two
    issues. I have simple dummy class called base which (everything is
    inlined just for readability) defines these methods...


    friend base operator+ (const base& lhs, const base& rhs)
    {
    base tmp;
    tmp.l = lhs.l + rhs.l;
    cout << "friend base operator+ (const base& lhs, const base&
    rhs)" << endl;
    return tmp;
    }

    base& operator+(const base& rhs)
    {
    cout << " base& operator+(const base& rhs) " << endl;
    this->l+=rhs.l;
    return *this;
    }


    I have put cout statements in my default ctor and copy ctor to see
    when they are called.

    Now in my main() function, I have a routine thus;


    base f(const base& b1, const base& b2)
    {
    cout << "base f(const base& b1, const base& b2)" << endl;
    base tmp = b1 +b2;

    return tmp;
    };



    My question is this. Within the function f above, I would expect to
    see output indicating;

    1) that the friend base operator+ (const base& lhs, const base& rhs)
    function will be called first.

    2) then the copy ctor will happen for base tmp = b1 +b2;

    3) then a copy ctor will be called for the return tmp statement.

    4) default dtor called for the temporary created upon f exit.


    thats not what I'm seeing however, my gnu c++ compiler is showing;

    1) default ctor called for base tmp = b1 +b2;
    2) followed by friend base operator+ (const base& lhs, const base&
    rhs) function


    I don't see the ctor being called for the return value followed by the
    dtor for the local tmp variable. Here's my thinking....

    1) when doing declaration and assignment on the same line, the copy
    ctor is called only if the rhs of "=" contains a single value. If
    there's any operations to be done, then the default ctor is used and
    then thats assigned into. i.e. base tmp = b1 +b2 doesn't call copy
    ctor but base tmp = b2; would

    2) The compiler is doing some funky optimisation wrt the copy of tmp
    being made for the return value. This point is the one that concerns
    me most. (yes, all my cout statements use endl at the end to flush the
    buffer.



    thanks a million

    GrahamO
  • Karl Heinz Buchegger

    #2
    Re: hmm.... two things to clarify...

    grahamo wrote:[color=blue]
    >
    > thats not what I'm seeing however, my gnu c++ compiler is showing;
    >
    > 1) default ctor called for base tmp = b1 +b2;
    > 2) followed by friend base operator+ (const base& lhs, const base&
    > rhs) function
    >
    > I don't see the ctor being called for the return value followed by the
    > dtor for the local tmp variable. Here's my thinking....
    >
    > 1) when doing declaration and assignment on the same line, the copy
    > ctor is called only if the rhs of "=" contains a single value. If
    > there's any operations to be done, then the default ctor is used and
    > then thats assigned into. i.e. base tmp = b1 +b2 doesn't call copy
    > ctor but base tmp = b2; would
    >
    > 2) The compiler is doing some funky optimisation wrt the copy of tmp
    > being made for the return value. This point is the one that concerns
    > me most. (yes, all my cout statements use endl at the end to flush the
    > buffer.[/color]

    2) is true.
    The compiler is allowed to optimize away the copy ctor. It is explicitely
    allowed to do so, even if the copy ctor contains side efefects, like
    your output statements.


    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Sharad Kala

      #3
      Re: hmm.... two things to clarify...


      "grahamo" <graham_walsh50 @hotmail.com> wrote in message
      news:79528aa8.0 402250409.6ea02 8b0@posting.goo gle.com...

      </snip>[color=blue]
      > 2) The compiler is doing some funky optimisation wrt the copy of tmp
      > being made for the return value. This point is the one that concerns
      > me most. (yes, all my cout statements use endl at the end to flush the
      > buffer.[/color]

      Reference to Standard 12.8/15
      "Whenever a temporary class object is copied using a copy constructor, and this
      object and the copy have the same cv-unqualified type, an implementation is
      permitted to treat the original and the copy as two different ways of referring
      to the same object and not perform a copy at all, even if the class copy
      constructor or destructor have side effects.For a function with a class return
      type, if the expression in the return statement is the name of a local object,
      and the cv-unqualified type of the local object is the same as the function
      return type, an implementation is permitted to omit creating the temporary
      object to hold the function return value, even if the class copy constructor or
      destructor has side effects. In these cases, the object is destroyed at the
      later of times when the original and the copy would have been destroyed without
      the optimization"

      -Sharad


      Comment

      Working...