Is it ok to refer to a member of a const reference on the right side of the assignment?

Kevin P. Fleming
Mon Sep 20 15:15:00 GMT 2010

On 09/20/2010 12:20 AM, Patrick Horgan wrote:
> This gives a seg fault on the access to the model member of op, since at
> that point the &op is 0.
>                    const
>                    operand&    op = (*top->operands).select(0, op.model);
> Should this work?  Can you refer on the right side of the assignment to
> the const reference?

You aren't asking the right question. It doesn't matter whether 'op' is
a const reference, a regular pointer, or something else... if you
reference it before it has been initialized, you will get undefined

I'm actually surprised the compiler will even compile this, but I guess
since C/C++ define that local variables are in scope in the entire block
in which they are declared, this is allowed... even though it can't work

Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
skype: kpfleming | jabber:
Check us out at &

More information about the Gcc-help mailing list