Uncallable delegates

FeepingCreature feepingcreature at gmail.com
Wed May 13 04:47:50 UTC 2026


On Wednesday, 13 May 2026 at 04:41:37 UTC, Walter Bright wrote:
> On 5/12/2026 7:31 PM, FeepingCreature wrote:
>> ```
>> @safe:
>> 
>> class C {
>>    int x;
>>    void foo() const { meep(); }
>> }
>> 
>> C c;
>> void meep() { c.x ++; }
>> 
>> void main() {
>>    auto c = new C;
>>    .c = c;
>>    c.foo; // c.x mutates as an effect of a const call!
>> }
>> ```
>> 
>> As far as I can tell this is totally unstoppable as global 
>> functions don't have const anyways. We'd need to consider the 
>> global scope as an implicit parameter so that it could be 
>> covered by constness even with global functions.
>
> A const reference to something can also have a mutable 
> reference to it at the same time. meep() is using a mutable 
> path to c.

yes that's what I'm saying, just because you're calling a const 
method doesn't mean you can be assured that the class you're 
calling it on won't be mutated as an effect on the call, only 
that it won't be mutated *via the passed context pointer.* that's 
why I think the example case given earlier with the delegate as a 
class field shouldn't be considered relevant.


More information about the dip.development mailing list