counting constructor calls

I counted 1 default ctor, 2 copy ctor and 1 assignment calls for the following statements and I am wondering for what 2 copy ctor calls are necessary?

T t503; t503 = g<T>(t3);

Where T and g are given by

class T{public: int i;T(int i):i(i){};/*some more member funcs helping counting*/};

template <class Type>
const Type g(Type t){return t;}

and t3 is given by T t3(3);
The default construct happens when you do T t503;

Another constructor is called when you do T t3(3); -- but this is not a default or copy construct.

The first copy constructor is called when you pass t3 as a parameter to the g() function. Since you are passing by value, you are actually passing a copy of the object.

The second copy constructor is called when you return t from the g() function.

The assignment operator call is obvious.


Note that in an optimized build, it's very likely that both copy constructors can be optimized away resulting in 0 copy ctor calls. But it depends on the circumstances.
Thanks!
Topic archived. No new replies allowed.