Why can't we define re-assignable const reference variable?

Janice Caron caron800 at googlemail.com
Sat Feb 16 14:20:27 PST 2008


On 16/02/2008, Christopher Wright <dhasenan at gmail.com> wrote:
> >> What's the difference (save polymorphism) between a pointer to a struct
> >>  and a reference to an object?
>
> In C++, there's no difference between the two.

Yes there is.

    S * p = whatever;
    S & r = whatever;

p can be rebound. r can't.

> In D, the difference
> appears to be that there is no syntax separate the reference from the
> object, besides an ugly cast.

And nor is there in C++. You cannot modify r. There is no way to get
at r. You can't even take the address of r. C++ prohibits you, and no
one's ever complained that that's a problem. If you want to rebind,
you use a pointer.


> I've never heard any explanation of how reassignable const objects would
> break the type system,

Because you'd need a brand new syntax to do it, and whatever syntax
you chose, it would break generic programming.


> and it seems to me that, if they would, then you
> shouldn't be able to do:
> struct Foo{}
> const(Foo)* fptr;

There's no problem with that. fptr is a pointer, not a reference.



More information about the Digitalmars-d mailing list