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

Sönke Ludwig ludwig at informatik_dot_uni-luebeck.de
Sun Feb 17 13:44:00 PST 2008


Janice Caron schrieb:
> 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.
> 
> Also, remember that a C++ class is a D struct, and there's really no
> equivalent to a D class in C++.

Show me how I can maintain a simple map of class instances, which I get as 
const(C) from somewhere else, i.e. I have to accept the const here.

example:

const(C)[string] map;

<- completely useless - you can do nothing with this construct

The only possibility to solve this is to either use a cast or to use a struct S 
{ const(C) obj } and modify the map to S*[string] (or something similar) - at 
least I don't see an alternative.

This, of course, would also apply to some self-made map-like container.



More information about the Digitalmars-d mailing list