Logical const

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 30 11:30:48 PST 2010


On Tue, 30 Nov 2010 14:07:51 -0500, Walter Bright  
<newshound2 at digitalmars.com> wrote:

> Steven Schveighoffer wrote:
>> What guarantees?  Const provides no guarantees.
>
> 1. That nobody will modify any of the data structure accessed through  
> the const reference provided. Thus, if your data is immutable, unique,  
> or the function is pure, you are guaranteed it will not be modified.

If your data is immutable it will not be modified.  That is a contract of  
immutable, not a const function.
If the function is pure, and *all* arguments to the function are const,  
the data wil not be modified, that is verifiable.
If the data is unique is not enforced by the compiler, so it's up to you  
to ensure this.  Sounds like a convention to me.

The example that I gave does not seem to you like it would surprise  
someone?  I passed in a const object and it got modified, even though no  
casts were used.

If you add a portion to the object that is mutable or head-const, this  
alters the rule, but does not detract from the guarantees.  The rule then  
just contains an exception for the mutable portion.

> 2. That another thread will not be modifying any of that data structure.

That rule is not affected by logical const.

-Steve


More information about the Digitalmars-d mailing list