const as default for variables

Zach the Mystic via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 16 07:40:50 PDT 2015


On Sunday, 15 March 2015 at 20:09:56 UTC, deadalnix wrote:
> On Sunday, 15 March 2015 at 07:44:50 UTC, Walter Bright wrote:
>> "const ref" can tell the optimizer that that path for a ref 
>> counted object cannot alter its ref count.
>
> That is not clear why. const ref is supposed to protect against 
> escaping when ref does not ?

There are two cases here. One is when the reference is copied to 
new variable, which would actually break const because the 
reference count of the original data would have to be incremented 
(which is a separate issue). But the other case is where the 
original is reassigned, in which the counter for the data it used 
to point to gets decremented, possibly to zero. `const` would 
guarantee against this. But even this is a blunt force weapon, 
because it would also stop you from mutating the original data, 
even though that wouldn't change the reference count.


More information about the Digitalmars-d mailing list