Why can't we define re-assignable const reference variable?
Janice Caron
caron800 at googlemail.com
Sun Feb 17 10:36:05 PST 2008
On 17/02/2008, Sergey Gromov <snake.scaly at gmail.com> wrote:
> Janice Caron <caron800 at googlemail.com> wrote:
> > On 17/02/2008, Sergey Gromov <snake.scaly at gmail.com> wrote:
> > > So it's really strange that you can't declare
> > > const(C) x;
> >
> > ???
> >
> > You can!
>
> But it wouldn't mean mutable reference to const data.
Of course not. That would be impossible.
You cannot have a mutable reference to const data in C++, so what's
the big deal? The best you can achieve in C++ is a mutable /pointer/
to const data, and that is also allowed in D.
> This /looks/ like
> const only affecting C
const is transitive, so it affects C, and everything referenced by C.
It declares the four-byte reference on the stack that is C, to be
const, and since const is transitive, that makes the instance of C's
data on the heap const, and everything referenced therefrom.
> but, in fact, constness is expanded over the
> whole declaration.
Of course. Everything in the brackets is const. That's what's brackets
are for - precedence. C is in the brackets; C is const.
> I.e. the syntax for declaring reference to const is
> already in the language,
You're going to have to clarify that, I'm afraid, as I don't
understand. Are you referring to the function parameter syntax
ref const(C) c
? In this case, c is indeed rebindable, but only because an extra
level of indirection has been silently added.
> the question is, why it's not in the type
> system.
I don't agree that that's even a reasonable question, because there is
/not/ a syntax for "mutable reference to constant data" (not counting
function parameters).
More information about the Digitalmars-d
mailing list