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

Sean Reque seanthenewt at yahoo.com
Sun Feb 17 15:34:32 PST 2008


> On 17/02/2008, Matti Niemenmaa <see_signature at for.real.address> wrote:
> > Now what he wants is to convert the C++ "const C* b = a;" to D "??? b = a;" with
> > the semantics that b can be reassigned but the data behind it is unchangeable.
> 
> OK, I see that.
> 
> But the thing is, it is never actually /necessary/ to do that. It is
> always possible to just write the code differently. There is really no
> pressing /need/ for "const class with mutable reference", since nobody
> has ever demonstrated a real-life use case that couldn't just be done
> differently. Certainly, no need pressing enough to demand a major
> change to generic programming and the type system.

Having a const class with mutable reference makes the language much much cleaner. The D docs themselves state that D is designed so that the programmer should rarely have to dip down to the level of pointers. Yet here we have a very important feature, const, that when used with an object reference cripples the reference so that it can no longer point to anything else, requiring us to use pointers when references should be able to suffice.

D references are like Java references, and D const has similiarities with C++ const. I don't see why it should be so hard to mix Java-style references with C++ const, especially since Java style references are nothing more  than syntactically pleasing pointers.



More information about the Digitalmars-d mailing list