the const correctness of the this pointer
Steven Schveighoffer
schveiguy at yahoo.com
Mon Dec 28 08:14:52 PST 2009
On Mon, 28 Dec 2009 10:50:02 -0500, Jason House
<jason.james.house at gmail.com> wrote:
> Steven Schveighoffer Wrote:
>
>>
>> This might be a difficult thing to fix, but it definitely *definitely*
>> needs to be fixed. The problem is that a delegate stores a function
>> pointer and a context pointer. However, it does not type the context
>> pointer. For example, if you do this:
>>
>> import std.stdio;
>>
>> class A
>> {
>> void f() const {}
>> }
>>
>> void main()
>> {
>> const A a = new A;
>> a.f();
>> auto g = &a.f;
>> writefln("%s", typeof(g).stringof);
>> }
>>
>> You will get this:
>>
>> void delegate()
>>
>> The this pointer is hidden, and so is it's const decoration. I would
>> expect to see:
>>
>> void delegate() const
>>
>> I'll file a bugzilla request for this.
>>
>> -Steve
>
>
> I disagree. Once the delegate is passed off to some other region of
> code, why should that other code care that there's an object that might
> not get modified from using the delegate? Especially when you consider
> that which object or stack frame is not tracked by the type system. Pure
> delegates make sense to me though.
It could be part of the interface for a function that takes a delegate
(i.e. I require that this delegate does not modify it's 'this' parameter).
But I agree, there could be issues -- if you don't care what the delegate
does (const or not), then how do you specify that?
I suppose we need delegate contravariance at that point -- a delegate to a
const function should implicitly casts to a delegate of a non-const
function. See http://d.puremagic.com/issues/show_bug.cgi?id=3075
This might be a good reason to revive that enhancement.
BTW, you also need to correctly mark shared functions, which *do* matter
as far as delegates go, since you need to synchronize access to the 'this'
pointer before calling the delegate.
-Steve
More information about the Digitalmars-d
mailing list