Fully transitive const is not necessary

Sean Kelly sean at invisibleduck.org
Wed Apr 2 15:55:24 PDT 2008


== Quote from Walter Bright (newshound1 at digitalmars.com)'s article
> If you do away with transitive const, you cannot have invariant either.
> Const allows you to reuse code that works on invariants to work with
> mutables, too.
> Logical const just utterly breaks the whole system. Every scheme we've
> looked at for logical const winds up in some way breaking invariants. If
> invariants are broken, the advantages of them all just go away. I
> suspect that logical const is like perpetual motion - if you think
> you've solved it, you've just made a mistake somewhere <g>.

My traditional argument in support of logical const is this:

    class C
    {
        mutable mutex monitor;
        std::string name;
    public:
        std::string name() const
        {
            scoped_lock sl( monitor );
            return name;
        }

        void name( std::string const& n )
        {
            scoped_lock sl( monitor );
            name = n;
        }
    };

Here, the mutex has nothing to do with the state of the object and
must be modified even during logically non-modifying operations.
Similar behavior might be necessary for a logging system in some
instances, etc.  However, I'm not certain whether this is sufficient
to justify logical const in general.  As you say--it has some serious
problems as well.


Sean



More information about the Digitalmars-d mailing list