While reading about the NRV optimization I learnt that one criteria
for this to be enabled is if the class has either an explicit or an
implicit copy constructor defined.
In general for a class T such as
and a Non member friend fuction of the class T such asQuote:}
T operator+(const T& a, const T& b)
If the class T has a copy constructor the NRV will be applied in theQuote:}
non member function such as the above, transforming the above
nonmember function to something like (name mangling et all left out
void T operator+(T& _result, const T& a, const T& b)
_result.T::T(); //default constructor. I assume one exists...
_result._x = a._x + b._x;
_result._y = a._y + b._y;
Now the question is NRV gets enabled if and only if the copyQuote:}
constructor exists..However the NRV optimization as above never uses
the copy constructor !! So why the requirement for NRV that
stipulates the existence of a copy constructor which it does not
use...It seems a bit contradictory to me in the sense it requires a
copy constructor but never uses it so the question is why require it
then !! I am sure there is a very good rationale for this but I have
not been able to come up with this rationale..Would appreciate if
anyone could shed more light on this...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]