Const Ideas
Christopher Wright
dhasenan at gmail.com
Mon Dec 3 10:09:38 PST 2007
Craig Black wrote:
> "Janice Caron" <caron800 at googlemail.com> wrote in message
> news:mailman.229.1196702116.2338.digitalmars-d at puremagic.com...
>> On 12/3/07, Craig Black <cblack at ara.com> wrote:
>>> Just to expound further, the pointer and reference syntax seems
>>> difference
>>> since the reference is implicit. Just think of "const X x;" as "const
>>> X&
>>> x;" and you will see that the same principle that applies to pointers
>>> should
>>> also apply to references.
>> Yes, I think we're on the same page here. Of course, only classes have
>> references. Structs don't, ints don't, etc., but for classes, my
>> preference would be for all four of the following to be exactly
>> equivalent:
>>
>> const C c;
>> const(C) c;
>> const C& c;
>> const(C&) c;
>>
>> (Which leaves open the possibility of "const(C)& c;" to mean mutable
>> ref to const data").
>>
>> That's not the status quo, however
>
> That syntax would looks OK, but I think it also makes sense to make
> "const(C) c" equivalent to "const(C)& c;". Walter said that this is how the
> syntax already works. Am I misunderstanding something?
We don't like the syntax. It makes a tiny bit of sense, with that
description, but it's unintuitive and error-prone.
> What I don't understand is why Walter and others think that "C const c"
> violates transitivitiy. What do they mean by transitivity?
"C const c" -> c is a pointer that cannot be reassigned, but you can
modify its members.
This violates transitivity, the property that you cannot follow a series
of const pointers/references and end up with something mutable. It is
also useful, but it isn't useful as a public interface sort-of-thing;
it's useful at a relatively small scope. Which is why Walter hasn't
implemented it; he doesn't see enough of a benefit yet.
More information about the Digitalmars-d
mailing list