What is the difference between...

Sean Kelly sean at f4.ca
Fri Sep 7 08:06:06 PDT 2007


Sean Kelly wrote:
> Sean Kelly wrote:
>> Daniel Keep wrote:
>>>
>>>> (2) void f(const(int*) x)
>>>
>>> Ok, now you've got a mutable read-only view: the bits of 'x' can be
>>> changed, but the data referenced by x cannot.
>>
>> I thought that's what 'final' was for.  For the above, I'd expect the 
>> reference and the data to which it refers to both be immutable through 
>> x.  Is this not right?
> 
> er, let me clarify.  I would assume that:
> 
> const(int)* x
> 
> means that the data cannot be changed but the pointer can.  But by 
> enclosing the pointer in parens, it should be considered constant as well.

Forget it, I remember now.  The parenthesis means "apply this attribute 
to the type herein" and const means "can't change the data."  Out of 
curiosity, how do things work with multiple attributes:

final const(int*)* x
const final(int*)* x

I assume that both of the above are equivalent, and they mean "mutable 
pointer to an immutable pointer to immutable data."  ie. all of the 
attributes together apply to the type in parens.  Is this correct?  I'll 
admit I still find the C++ version easier to visually parse for 
non-trivial declarations--the rule is simply "apply this attribute to 
the adjacent thing on the left."  But perhaps this is because I've spent 
more time with C++ const than D const.


Sean



More information about the Digitalmars-d mailing list