Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 4 06:24:31 PDT 2008


"Walter Bright" wrote
> Steven Schveighoffer wrote:
>> "Walter Bright" wrote
>>> Sure, but static class members are *not* part of the transitive closure 
>>> state of the object. They're just global variables.
>>
>> Neither are mutable member variables.
>
> How can a member not be a member of the state of an object?

The very act of declaring it mutable tells the compiler "this is not object 
state."  I don't like the keyword mutable, what about 'nonstate' or 
something like that.  It's not really saying "this will always be mutable", 
it's just saying "this is not part of the object state, so don't apply const 
casts to it," which happens to mean it's mutable when the rest of the object 
is const or invariant.

Declaring a nonstate member is basically a way to associate data with an 
object that is not owned by the object.  For example, a cache in a 
calculator object may be used by the methods of the object, but it's not 
'owned' by anything, just like the methods of the object can use global 
data, or data from the arguments passed to the function.  I like to think of 
nonstate data as extra arguments passed to every method call, in addition to 
the 'this' pointer.  In fact, this could be how it was implemented in the 
compiler (but I think implementing it inline with the class would have 
better performance):

class C
{
   // this is like a struct passed as an argument to all methods, and is 
always mutable
   nonstate
   {
       int c;
   }

   // is passed an implicit nonstate pointer to the nonstate values
   int f(int x) const
   {
       return nonstate.c++;
   }
}

This way, the nonstate part could be laid out in the same memory chunk as 
the class, and is passed around with the class, but is not technically part 
of the class.  This is similar to how a mutex is associated with a class, 
lives in the same memory space as the class, but is not considered part of 
the transitive const closure of the object.  There could be a vtable entry 
that points to the nonstate data, so the layout would move with derived 
classes.

-Steve 





More information about the Digitalmars-d mailing list