Re: Overload resolution and copy constructors that take non-const references
by dizzy <dizzy@[EMAIL PROTECTED]
>
May 7, 2008 at 05:47 PM
Victor Bazarov wrote:
> Branimir Maksimovic wrote:
>> On May 7, 3:21 pm, Branimir Maksimovic <bm...@[EMAIL PROTECTED]
> wrote:
>>> What about
>>> A foo()
>>> {
>>> A a;
>>> return a;}
>>>
>>> A a;
>>> a = foo();
>>> Is compiler obliged to call copy constructor?
>>
>> Err, I mean to construct tem****ary.
>
> Yes, with assignment it would have to create a tem****ary (and BTW
> I really hope the 'a = foo();' statement is inside some function
> otherwise it would be ill-formed). There are no provisions in the
> Standard to allow skipping creating a tem****ary when one would be
> required because the expression on the right-hand side of the
> assignment operator produces it. The creation of the tem****ary
> is only allowed to be skipped in the case of initialising an object
> of the same type:
>
> A a = foo();
I think there was one more case where copy ctor elusion is permited. I
would
also like to add that this is special treatement in the standard for these
cases even if the copy ctor has observable behaviour altering code.
Because
in general if calling or not calling the copy ctor results in the same
observable behaviour (which is to be expected to most copy ctors that
usually don't have side effects) then the compiler can optimize out the
call anyway no matter of being in such a special case or not :)
--
Dizzy