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

Sergey Gromov snake.scaly at gmail.com
Sat Feb 16 18:36:18 PST 2008


none <z at gg.com> wrote:
> == Quote from Janice Caron (caron800 at googlemail.com)'s article
> > On 16/02/2008, none <z at gg.com> wrote:
> > >  So why can't we have both (just as in C++):
> > This has been covered so many times before, but in summary, allowing
> > that would completely break the type system.
> 
> I don't understand why it will break the type system; in any case, even without it
> (as we do now), I can always get around it by using D pointers:
> 
>   const(B)* b;            // type change
>   b = &b1;                // rebind
>   b = &b2;                // rebind
> 
> It's just so inconvenient, that's why I suggest allow define re-assignable const
> reference variable.
> 
> > But I just want to clear up one misconception. You /cannot/ do that in
> > C++. References in C++ are /never/ rebindable.
> 
> That's a terminology issue, to make it clear:
> 
> class C {}
> 
> D                           C++
> =========================   ===========================
> reference: C c = new C();   pointer:    C *c = new C();
> pointer:   C* cp = &c;      ptr-to-ptr: C **cp = &c;

This is not how the things currently are.  Actually it's like this:

class C {}
struct S {}

D                           C++
=========================   ===========================
reference: C c = new C();   pointer:    C *c = new C();
pointer:   C* cp = &c;      pointer:    C *cp = c;
value:     S s = {};        value:      S s = {};
pointer:   S* sp = &s;      pointer:    S *sp = &s;
no equiv.                   reference:  C &cr = *c;
no equiv.                   reference:  C &sr = s;

So it's really strange that you can't declare
const(C) x;
All the syntax is here already.

-- 
SnakE



More information about the Digitalmars-d mailing list