const by default.

BCS BCS at pathlink.com
Fri Jul 7 11:20:07 PDT 2006


Walter Bright wrote:
[...]
 > class C { int m; }
 > void bar(C c) { c.m = 3; } // ok
[...]
> 
> C foo(in C c) { return c; } // ok
> 
> void foo(in C c) { bar(c); } // ok
> 
> void foo(in C c)
> {   C d = c;
>     d.m = 3;  // ok
> }
> 

How would the last three work? IIRC, const implies that data is not 
accessible for writes. In the first of those three, the return value has 
no indication of const'ness so something like this could be done

void bad(in C c)
{
	c.m = 0;	//error
	foo(c).m = 0;	//same effect, but OK
}

The next one has the same effect, bar changes the parameter, where as 
the calling function isn't allowed to.

The last one again lets a function change something it shouldn't.



I feel like I'm missing something here, these seem to obvious for you to 
have missed. Maybe I'm looking for something other than what you are 
trying for.

What I want is a way to get an assurance by the compiler that a 
referenced value will not be changed. This would require that const 
references (or a non mutable ones) can never be made into a mutable 
references.

If I'm missing something, please point it out.



More information about the Digitalmars-d mailing list