What guarantees does D 'const' provide, compared to C++?

Era Scarecrow rtcvb32 at yahoo.com
Sun Aug 19 12:58:10 PDT 2012


On Sunday, 19 August 2012 at 19:42:20 UTC, Peter Alexander wrote:
> On Sunday, 19 August 2012 at 19:26:58 UTC, Jesse Phillips wrote:
>> While in context with the original question this is fine, but 
>> I do not like this use of guarantee.
>>
>> What I mean is, const does provide guarantees by itself. And 
>> it provides more than C++ because it is transitive and 
>> modifying a const reference is undefined.
>
> What guarantees does const provide on its own?

  If you don't circumvent the language by casting/forcing it, then 
const (and immutable) items cannot be changed by the functions 
called with them, nor them nor anything they contain or reference.

  In cases of methods to classes/structs, const ensures the object 
(allocated separately in each variable/heap location) won't 
change in any way.

  Isn't that good enough?

  It's like asking what guarantees a book contains it's data. It's 
entirely guaranteed as long you don't use a permenant marker, rip 
the pages out, burn it, soak it in a bath tub full of water or 
acid, run it over, throw it in lava, bleach it, or send it 
through a trans-dimensional portal to another universe. or any 
number of other things, but that's outside the scope of the 
publisher's ability to after providing the book; when left 
untouched the book should always contain the data.

  Isn't that good enough?


More information about the Digitalmars-d mailing list