temporary and constructor

Discussion in 'C++' started by Neelesh Bodas, Nov 18, 2005.

  1. Hello all, I had a confusion regarding temporaries :

    #include <iostream>
    class X {
    public:
    X() { std::cout << " ctor " << std::endl; }
    ~X() { std::cout << " dtor " << std::endl; }
    X(const X&) { std::cout << " copy ctor " << std::endl; }

    };

    int main()
    {
    { // nested scope to see dtor getting called.
    X(X()); // not really a good code, but just to get concepts
    thoroughly cleared.
    }

    }
    There is no output produced. (g++ 3.4.2)

    The standard says :
    12.2 - 3 : When an implementation introduces a temporary object of a
    class that has a nontrivial constructor (12.1), it shall ensure that a
    constructor is called for the temporary object. Similarly, the
    destructor shall be called for a temporary with a nontrivial
    destructor (12.4).
    12.2 - 5: A temporary bound to a reference parameter in a function call
    (5.2.2) persists until the completion of the full expression containing
    the call.

    So it appears that the ctor and destructors for class X must be called.

    So the output above still remains unexplained.
     
    Neelesh Bodas, Nov 18, 2005
    #1
    1. Advertising

  2. Neelesh Bodas

    Guest

    Neelesh Bodas wrote:
    > Hello all, I had a confusion regarding temporaries :
    >
    > #include <iostream>
    > class X {
    > public:
    > X() { std::cout << " ctor " << std::endl; }
    > ~X() { std::cout << " dtor " << std::endl; }
    > X(const X&) { std::cout << " copy ctor " << std::endl; }
    >
    > };
    >
    > int main()
    > {
    > { // nested scope to see dtor getting called.
    > X(X()); // not really a good code, but just to get concepts
    > thoroughly cleared.
    > }
    >
    > }
    > There is no output produced. (g++ 3.4.2)
    >
    > The standard says :
    > 12.2 - 3 : When an implementation introduces a temporary object of a
    > class that has a nontrivial constructor (12.1), it shall ensure that a
    > constructor is called for the temporary object. Similarly, the
    > destructor shall be called for a temporary with a nontrivial
    > destructor (12.4).
    > 12.2 - 5: A temporary bound to a reference parameter in a function call
    > (5.2.2) persists until the completion of the full expression containing
    > the call.
    >
    > So it appears that the ctor and destructors for class X must be called.
    >
    > So the output above still remains unexplained.


    I can't remember all the details of the syntax rules, but I believe
    (and the warning from VC++8 would suggest) that

    X(X());

    is actually a function declaration and so your code never does
    anything. Change it to

    X(X);

    That gave expected results for me (although, since the copy constructor
    was elided, the results don't help your experiment).

    Gavin Deane
     
    , Nov 18, 2005
    #2
    1. Advertising

  3. * Neelesh Bodas:
    > Hello all, I had a confusion regarding temporaries :
    >
    > #include <iostream>
    > class X {
    > public:
    > X() { std::cout << " ctor " << std::endl; }
    > ~X() { std::cout << " dtor " << std::endl; }
    > X(const X&) { std::cout << " copy ctor " << std::endl; }
    >
    > };
    >
    > int main()
    > {
    > { // nested scope to see dtor getting called.
    > X(X()); // not really a good code, but just to get concepts
    > thoroughly cleared.
    > }
    >
    > }
    > There is no output produced. (g++ 3.4.2)



    That's because you're declaring a function X that returns X.

    Try

    (X(X()));

    ;-)

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 18, 2005
    #3
  4. Neelesh Bodas

    Gregory Guest

    Yes, X(X()); is interpreted by g++ compiler as X X() (external brackets
    are removed) -
    declaration of function named X that returns class X and has no
    parameters.
    Is it correct interpretation should be consulted with the standard.

    Write:

    X(X())
    {
    }

    and compile with g++ you'll get something like this:

    x.cpp: In function `X X()':
    x.cpp:12: warning: control reaches end of non-void function

    Gregory
     
    Gregory, Nov 18, 2005
    #4
    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. Andy Buckley
    Replies:
    5
    Views:
    341
    Victor Bazarov
    Aug 26, 2004
  2. Replies:
    5
    Views:
    356
    Victor Bazarov
    Jun 24, 2005
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,253
  4. Replies:
    7
    Views:
    3,231
    James Kanze
    Feb 12, 2008
  5. Marcelo De Brito

    Copy Constructor And Temporary Objects

    Marcelo De Brito, Mar 1, 2009, in forum: C++
    Replies:
    2
    Views:
    503
    Kai-Uwe Bux
    Mar 1, 2009
Loading...

Share This Page