By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,428 Members | 1,849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,428 IT Pros & Developers. It's quick & easy.

temporary and constructor

P: n/a
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.

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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 22 '05 #2

P: n/a
* 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?
Nov 22 '05 #3

P: n/a
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

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.