Fully transitive const is not necessary

Janice Caron caron800 at googlemail.com
Wed Apr 2 00:16:42 PDT 2008


On 01/04/2008, Craig Black <craigblack2 at cox.net> wrote:
> Walter/Andrei are under the impression that transitive const is necessary
>  for multiprogramming, but they have not presented a detailed explanation
>  about why it is necessary.

It hardly needs a detailed explanation. In fact it's trivial to
understand. Watch:

    // in C++
    void f(const C &c)
    {
        c.some.deeply.nested.but.reachable.var++;
    }

In a multithreaded program, a thread switch may occur between the read
and the write of the read/write cycle that is ++. If that happens,
you're screwed.

    // in D
    void f(const C c)
    {
        c.some.deeply.nested.but.reachable.var++; /*ERROR*/
    }

In D, such dodgy code just won't compile.


>  It sounds like a more detailed explanation of
>  the merits of transitive const from Walter/Andrei would help here.

Was that not detailed enough for you? It's not rocket science.


>  One idea that I had recently was that the const keyword could provide
>  logical const

Logical const is mutually incompatible with transitive const. If an
object is transitively const, then it means "I promise not to modify
anything reachable through this pointer", wheras if an object is
logically const, it means that some bits exists which are reachable
throgh the pointer, but which you are allowed to modify. The two
promises:

(1) "I promise not to modify anything reachable through this pointer"
(transitive const)

(2) "I promise not to modify anything reachable through this pointer,
except - oh wait! I might modify these bits here" (logical const)

cannot both be simultaneously true. Multiprogramming needs transitive
const. It's that simple.



More information about the Digitalmars-d mailing list