Fully transitive const is not necessary

Walter Bright newshound1 at digitalmars.com
Wed Apr 2 16:28:44 PDT 2008


Sean Kelly wrote:
> 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.

If C.name were invariant, there would be no need for any locks. I don't 
think this is a good example, because with D's invariant strings there 
is no need for such locking.



More information about the Digitalmars-d mailing list