How to break const

Christophe Travert travert at phare.normalesup.org
Wed Jun 20 04:36:10 PDT 2012


Timon Gehr , dans le message (digitalmars.D:170288), a écrit :
> On 06/20/2012 09:16 AM, deadalnix wrote:
>> Le 19/06/2012 17:49, Timon Gehr a écrit :
>>>
>>> The question is, what the meaning of 'const' references should be:
>>>
>>> 1. data cannot be changed transitively through the reference
>>>
>>> 2. the reference can reference both 'const' and 'immutable' data and
>>> 'immutable' data can transitively not be changed through the
>>> reference.
>>>
>>>
>>> 1. requires transitive const for delegate context pointers, 2. does not.
>>>
>>
>> No, 2. require 1., even if the initialization is broken.
>>
>> class Foo {
>>      void delegate() dg;
>>
>>      this(immutable void delegate() dg) immutable {
>>          thid.dg = dg;
>>      }
>> }
>>
>> Now, as delegate doesn't carry the constness of its context, an
>> immutable instance of Foo can refers to something that isn't immutable.
> 
> Clarification: 'const' means 'const'. No other qualifiers.
>
> There is no 'const' in that example code. 'immutable' obviously needs to 
> be transitive regardless of the particular interpretation of 'const'.
> 
const means: maybe immutable. Thus const needs to be transitive too. If 
you apply different rules to const and to immutable, you are breaking 
the consistency of the langage.



More information about the Digitalmars-d mailing list