Persistent list

Dicebot via Digitalmars-d digitalmars-d at puremagic.com
Sun Nov 15 07:41:31 PST 2015


On Sunday, 15 November 2015 at 15:20:24 UTC, Dicebot wrote:
> On Sunday, 15 November 2015 at 15:09:25 UTC, Jonathan M Davis 
> wrote:
>> After the call to bar, the compiler can guarantee that the 
>> value of foo has not changed, because it's not possible for 
>> bar to access foo except via a const reference, and it can 
>> clearly see that, because it can see that it's not possible 
>> for bar to access foo except via the reference that's passed 
>> to it.
>
> Note that it can only do so if escape analysis is able to prove 
> no other reference escapes to the same data. Otherwise it can 
> be mutated even during that function call in other thread. All 
> together it makes benefits of such deduction absolutely not 
> worth UB limitation in my opinion.
>
> For pure functions it is quite different story though and I 
> agree that demanding it to be UB there is totally legitimate.

Probably also worth mentioning that I don't consider use case 
like in Andrei example (casting away const to mutate 
allocator/refcount of const argument) a legitimate example as it 
is public API and guarantees of mutability of underlying object 
are actually impossible.

Use case I had myself it to cast away const inside invariants but 
this was only ok because we know for sure to never use immutable 
objects _within out own code_. In Phobos such casting is very 
unlikely to be future-proof.


More information about the Digitalmars-d mailing list