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