Why can't we define re-assignable const reference variable?
Walter Bright
newshound1 at digitalmars.com
Tue Feb 19 12:49:09 PST 2008
Sergey Gromov wrote:
> I'm talking from the standpoint of generalization which you so much care
> about. When I generalize, I don't care how things are implemented.
> It's behaviour that matters. If I create an algorithm that uses
> references to invariant data, then it will work with anything that
> /behaves/ like such references. Even if they are actually ints passed
> by value.
That's a good point, and a good reason why:
invariant(C)[] c;
invariant(int)[] i;
should both have immutable array contents, i.e. if the contents of a
reference is invariant then the reference itself should be invariant.
The concept of having a mutable reference to immutable data is
fundamentally flawed because it breaks the concept of what a reference
is. A reference is not separable from what it refers to. If they were
separable, they would be syntactically and semantically the same thing
as pointers, and there would be no point whatsoever to even having a
reference type.
More information about the Digitalmars-d
mailing list