unexpected copy constructor

Discussion in 'C++' started by ciccio, May 20, 2010.

  1. ciccio

    ciccio Guest

    Hi,

    I recently stumbled upon something rather annoying. When doing a quick
    return in a function, it is possible that a copy constructor is called.
    This is rather surprising.

    The code below demonstrates this and outputs the word "copy".
    Is this a bug in the compiler or something from C++? Such a copy
    constructors are often really not wanted.

    Thanks for the help.

    This was compiled with g++ version 4.4.3.

    #include <iostream>

    class foo {
    public:
    foo() {}
    foo(const foo&f) {std::cout << "copy" << std::endl;}
    };

    foo function(int i) {
    if (i > 0) return foo();
    foo f;
    return f;
    }

    int main(void) {
    function(-1);
    }
     
    ciccio, May 20, 2010
    #1
    1. Advertising

  2. ciccio

    Jonathan Lee Guest

    On May 20, 10:37 am, ciccio <> wrote:
    > I recently stumbled upon something rather annoying. When doing a quick
    > return in a function, it is possible that a copy constructor is called.
    > This is rather surprising.


    Actually, it's the normal behavior. How else is your "f" object
    supposed to get from the function scope to the containing scope?
    At least conceptually, it supposed to be destroyed at the end of
    its own scope. So a copy is made and the original destroyed.

    > The code below demonstrates this and outputs the word "copy".
    > Is this a bug in the compiler or something from C++? Such a copy
    > constructors are often really not wanted.


    You're right, they're not. C++ compilers are allowed to elide
    the copy, i.e., skip it. I guess it depends on the compiler, but
    basically they construct the value to be returned in the place
    where it is going to be returned to. Thus eliminating the copy
    (and destruction).

    You'll probably want to look into return value optimization
    (or just RVO). Also, check out the FAQ at parashift.com

    Whether or not RVO is performed seems to depend a lot on your
    compiler. One thing that can sometimes throw it off is if
    you are not returning the same object at all points in your
    function. Like, if every return statement returned "f" then
    you'd probably be okay. The fact that one place returns "f"
    and the other "foo()" might cause a problem.

    You gotta sorta test things yourself, or find your compiler's
    documentation that says specifically what triggers or blocks
    RVO.

    --Jonathan
     
    Jonathan Lee, May 20, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,902
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    468
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    579
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,246
  5. cinsk
    Replies:
    35
    Views:
    2,613
    James Kanze
    Oct 11, 2010
Loading...

Share This Page