This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Binding a temporary to a const reference
>>>>> Branko Cibej <brane@cthulhu.engr.sgi.com> writes:
> Joe Buck wrote:
>>
>> While I believe you are correct in saying that g++ is wrong in this case,
>> the protection rule is independent of whether or not the copy constructor
>> is actually called.
> Yes, I'm aware of that. (Seems to me there used to be a bug in g++
> regarding that, didn't there?)
g++ started enforcing this rule recently, but then the rule itself is
somewhat recent.
> Alexandre Oliva wrote:
>>
>> [dcl.init.ref]/8 says that, if the initializer expression is an rvalue
>> and the reference is of class type, an implementation is allowed (but
>> not required) to copy-construct another temporary and bind the
>> refenrence to it. In any case, the copy constructor must be
>> accessible [dcl.init.ref]/9. So g++ is right.
> Hmm. So what about the following? G++ accepts it without a diagnostic:
> class A {
> A (A const&);
> A operator = (A const&);
> public:
> A() {}
> };
> A const a;
> A const& ra = a;
Here, 'a' is an lvalue, so Alexandre's quote doesn't apply.
Jason