On Wed, May 21, 2008 at 07:14:27PM +0200, Stefan Schulze Frielinghaus wrote:
> Hello all,
>
> I just tried to easily show how many temporary objects get created in my
> program. I created a test application like this one:
> #include <iostream>
>
> struct A {
> static int c, d;
> A() { ++c; }
> ~A() { ++d; }
> void operator+=(const A&) { }
> A operator+(const A&) { return *this; }
> };
You forgot to add
A::A(const A&) { ++c;}
The missing call is to the copy constructor. Since you didn't declare
one, the compiler inserts one, and it doesn't increment the counter.
Also, there are situations where a C++ compiler is allowed to eliminate
temporaries (even if this changes side effects that are invoked by
a copy constructor).