Want reasonable reference counting? Disable automatic sharing of immutable

Dukc ajieskola at gmail.com
Mon Nov 15 09:22:16 UTC 2021


On Monday, 15 November 2021 at 08:38:07 UTC, Ola Fosheim Grøstad 
wrote:
> On Monday, 15 November 2021 at 07:14:12 UTC, Walter Bright 
> wrote:
>> On 11/14/2021 12:04 PM, H. S. Teoh wrote:
>>> Given D's emphasis on generic code, (1) seems like the only 
>>> viable
>>> option.  Which makes const in D even narrower in scope than 
>>> ever.
>>
>> C++ const doesn't actually work, which is why C++ ref counted 
>> objects can be const.
>
> Uh? C++ shared_ptr stores the count in a separate object.
>
> The reason you can cast away const is so that you can call 
> C-APIs that actually have const-behaviour, but not a const 
> signature.
>
> You have to be able to do this in D to.

I think he meant from optimisation viewpoint. C++ `const` is so 
weak that it can't be used for actual optimisations anyway, at 
least usually. So no performance is lost because of `mutable`. In 
D the compiler can make some assumption based on `const` and 
`immutable`, so we lose some performance potential if we 
implement `mutable`.


More information about the Digitalmars-d mailing list