Why is copy constructor called only twice here (should be thrice?)

Discussion in 'C++' started by TechCrazy, Jul 15, 2005.

  1. TechCrazy

    TechCrazy Guest

    I am compiling like

    g++ -O0 MyString.cpp

    Should the copy cnstrs be called thrice? Any optimization going on
    here?

    --------------------------
    #include <iostream>
    #include <string>
    using namespace std;
    class MyString: public string {
    public:
    MyString(const char * str): string(str) {
    cout << "copy ctr 1 called" << endl;
    }
    MyString(MyString const & str): string(str) {
    cout << "copy ctr 2 called" << endl;
    }
    };

    MyString getString() {
    MyString x("rrr"); //copy ctr 1 called

    //This should call copy ctr once to create the temporary
    //that will be returned
    return x;
    }

    int main() {
    //This should call copy ctr again with the temporary
    //created at the end of getString() to create y
    MyString y (getString());
    }

    ------------
    This program is printing:

    copy ctr 1 called
    copy ctr 2 called
    TechCrazy, Jul 15, 2005
    #1
    1. Advertising

  2. TechCrazy

    benben Guest

    "TechCrazy" <> wrote in message
    news:...
    > I am compiling like
    >
    > g++ -O0 MyString.cpp
    >
    > Should the copy cnstrs be called thrice? Any optimization going on
    > here?


    The getString() function is eligible for Named Return Value (NRV)
    optimization, which eliminates the temporary.

    Regards,
    Ben
    benben, Jul 15, 2005
    #2
    1. Advertising

  3. TechCrazy

    TechCrazy Guest

    But, I turned off all optimization with -O0
    TechCrazy, Jul 15, 2005
    #3
  4. TechCrazy

    Ian Guest

    TechCrazy wrote:
    > I am compiling like
    >
    > g++ -O0 MyString.cpp
    >
    > Should the copy cnstrs be called thrice? Any optimization going on
    > here?
    >

    Yes, the object returned by getString is y.

    > --------------------------
    > #include <iostream>
    > #include <string>
    > using namespace std;
    > class MyString: public string {
    > public:
    > MyString(const char * str): string(str) {
    > cout << "copy ctr 1 called" << endl;
    > }

    Not a copy constructor.

    Ian
    Ian, Jul 15, 2005
    #4
  5. TechCrazy

    benben Guest

    Ian, I think the OP wants to ask why the constructor was called twice than
    three times.

    ben
    benben, Jul 15, 2005
    #5
  6. TechCrazy wrote:
    >
    > But, I turned off all optimization with -O0


    Then ask the guys writing your compiler, why NRVO is still
    done in that case.

    I guess the answer will be something like: NRVO is always on, because
    it is an optimization which affects the callee as well as the caller.
    If we would allow to disable it, then there could be a situation, where
    the caller is compiled for NRVO, while the callee is not. And that will
    give you a big crash.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jul 15, 2005
    #6
    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. Robert
    Replies:
    6
    Views:
    507
  2. Replies:
    10
    Views:
    549
    Dave Townsend
    Oct 2, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,748
    Smokey Grindel
    Dec 2, 2006
  4. Alexander Stippler

    conversion constructor called twice - why?

    Alexander Stippler, Oct 30, 2005, in forum: C++
    Replies:
    3
    Views:
    347
    Valentin Samko
    Oct 31, 2005
  5. Manny
    Replies:
    9
    Views:
    224
    Manny
    Aug 30, 2003
Loading...

Share This Page