logical const is a subset of transitive const

Bill Baxter dnewsgroup at billbaxter.com
Fri Sep 14 13:07:17 PDT 2007


Janice Caron wrote:
> On 9/14/07, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
>> I disagree.  Declaring it const means that calling that function will not
>> change any members of the date object.
>>
>> Declaring it pure means that it will always return the same value, which
>> means it would be safe for multiple threads to access without locking (I am
>> very new to this concept, but I got all my knowledge from wikipedia:
>> http://en.wikipedia.org/wiki/Functional_programming)
> 
> I stand corrected. Yes, you are right. A function can be declared
> const and still read and write global variables, and hence not be
> threadsafe.

Whoa.  Excuse me while I get up off the floor after having the rug 
pulled out from under me.

Was the point of const then perhaps more micro-level optimizations? 
Things like if "X is const then I can assume the value in this register 
is still valid without having to re-fetch from main memory".  For instance
     X* foo;
     foo.bar = 10;
     func(foo);  // takes fully_const X*

     // In C++ const the next line requires re-fetching foo.bar
     // from memory,
     // because func might actually change fully_const X*.
     // In D it could reuse the value in register, or even assume it's
     // still the constant 10.

     Y biff = foo.bar;

Just a guess.  IANACW (compiler writer).

--bb



More information about the Digitalmars-d mailing list