Latest update...
Since there seemed to be so much concern using the printf and operator++ functions, I've reworked this experiment. Hopefully the following will appease those concerned...
Code:
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::hex;
class A
{
public:
A ()
{ cout << “\n A::A () executing: this = 0x” << this << endl; }
A (const A&)
{ cout << “\n A::A (const A&) executing: this = 0x” << this << endl; }
~A ()
{ cout << “\n A::~A () executing: this = 0x” << this << endl; }
A& fn ()
{ cout << “\n A::fn () executing: this = 0x” << this << endl;
return *this;
}
};
void fn (...)
{
cout << "\n fn (...) executing" << endl;
va_list ap;
va_start (ap, void);
A& a = va_arg (ap, A);
cout << "\n fn (...): &a = 0x" << &a << endl;
va_end (ap);
}
int main ()
{
cout << hex;
A a; fn (a.fn ());
cout << “\n\n End of main” << endl;
}
Execution of program...
A::A () executing: this = 0xffbee4db
A::fn () executing: this 0 0xffbee4db
A::A (const A&) executing: this = 0xffbee4da
fn (...) executing
fn (...): &a = 0xffbee4da
End of main
A::~A () executing: this = 0xffbee4db
End of execution!
The results are virtually identically to the original. So again, why is the 2nd creation of A never destroyed??? In case this may help, the following is the preprocessor output of void fn (...).
Code:
cout << "\n fn (...) executing" << endl;
va_list ap ;
( void ) ( ap = ( __va_list ) & __builtin_va_alist ) ;
A & a = ( ( A * ) __builtin_va_arg_incr ( ( A * ) ap ) ) [ 0 ] ;
cout << "\n fn (...): &a = 0x" << & a << endl ;
( void ) 0 ;
}
Earlier in the preprocessor output it shows va_list typdef'd as __va_list, and __va_list is typdef'd as void *. The __builtin_... functions are internal and so I cannot see how they are defined. However, the creation of the 2nd object (via the copy ctor) occurs prior to the execution of the fn (...) function. In fact, the code within this function is completely irrelevant. Comment out the code and the results are still the same (no destruction of the 2nd object). So here again, the compiler felt a need to create a new object, yet it felt no need to destroy it?!
By the way, this was not compiled using VC++ (someone had a question about that). This was compiled using Sun WorkShop 6 update 2 C++ 5.3.