Bug? access to copy-constructor for operator= needed in gcc-4.0

Nathan Sidwell nathan@codesourcery.com
Mon Jun 13 08:11:00 GMT 2005


Martin Kittel wrote:

>         // works fine
>         A a;
>         m_a = a;

>         // needs access to copy-constructor, why?
>         m_a = A();

This is about reference binding of parameters.  8.5.3 says how references are 
initialized.  in particular, 'a' is a non-temporary lvalue so the reference can 
be bound directly to it -- no copy ctor required.

'A()' is a temporary rvalue, so we can either
a) bind the reference directly to the rvalue, or
b) create a temporary (with the copy ctor) and then bind the reference to that.

(footnote 93 points out that option b will recurse so eventually we have to 
choose option a.)

Regardless of whether we immediately choose option a or not, the constructor we 
would have called had we chosen option b must be callable. This rule means your 
code will still compile on a lamer compiler that did actually copy the object, 
even if you developed it on a better compiler that immediatly bound the reference.

This final check is the 'surprise', and g++ is correct in performing it.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk



More information about the Gcc-help mailing list