temporary and constructor

N

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)

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

deane_gavin

Neelesh said:
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
 
A

Alf P. Steinbach

* 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()));

;-)
 
G

Gregory

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top