Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 2 07:24:14 PDT 2008


"Janice Caron" wrote
> On 02/04/2008, Steven Schveighoffer wrote:
>> "Janice Caron" wrote
>>  > pure requires transitive const.
>>
>>  Forgive me if I don't take your word for it.  Please prove this.
>>  Specifically, why pure fails with logical const (not head const as C++ 
>> is).
>
>    class Stupid
>    {
>        int x;
>        mutable int y;
>    }
>
>    not_pure void f(const(Stupid) stupid) const
>    {
>        int t = y;
>        ++t;
>        y = t;
>    }
>
> Try calling f from two threads at the same time, imagine a thread
> switch partway through f.

I'm assuming you meant f to be part of Stupid?  And that not_pure means that 
you are trying to make f pure, but this is your comment that it doesn't 
work?

This would not compile under the rules I specified.  Note the rules:

- a pure function can only access local variables (I'm going to ammend this 
to say local variables and globally invariant variables).
- all parameters to a pure function must be logically invariant
- a pure function can only call pure functions or methods.
- a pure method cannot access the mutable portion of a logically invariant
data value.

Your code violates rules 2 and 4.

-Steve 





More information about the Digitalmars-d mailing list