'var' and 'volatile' as !const, with invariant-by-default

BCS ao at pathlink.com
Mon Jun 25 14:01:38 PDT 2007


Reply to Russell,

> BCS wrote:
> 
>> unless all var value type are volatile this has a problem.
>> 
>> var int i = 3;
>> var volatile int* j = &i;
>
> This line is not legal, in my original rules, because when you take a
> pointer to a 'var' variable, you get a 'volatile' pointer.  You can't
> assign that to a 'volatile var' variable without an explicit cast.
>

So the only way to get a non read only reference is with a cast?

About half the time I use references, it it to get write access. Requiring 
an cast every time would be a major pain.

I'd make the '&' as permissive as possible and have the assignment to a non 
var pointer drop wright access. This sort of going with the idea that code 
should assert what it will and wont do, but the compiler shouldn't restrict 
it beyond that.

Hm. Here their be Interesting thoughts. ;)

> As for your example with the variables 'j' and 'jp' below, that would
> be exactly how my proposal would work (if 'volatile' were allowed on
> value-types).  In my original post, I said that if you take a pointer
> to a 'volatile var' you get a 'volatile var'.
> 
> So you & I are on the same wavelength. :)
> 
>> i=5; if(*j)... // ok j volatile
>> 
>> *j = 3; if(i)... // oops i is aliased
>> 
>> how about allow volatile on value types, and requiter it to get a non
>> read only reference to it
>> 
>> var int i = 3;
>> var volatile int j = 3;
>> auto ip = &i; // read only reference to i.
>> auto jp = &j; // read/write reference to j.
>> auto jp = ip; // invalid
>> 





More information about the Digitalmars-d mailing list