the const correctness of the this pointer
Steven Schveighoffer
schveiguy at yahoo.com
Mon Dec 28 06:25:50 PST 2009
On Sun, 27 Dec 2009 19:02:23 -0500, TM <ithink at therefore.iam> wrote:
> Hello,
> I was dealing with delegates when I found that the const correctness of
> the this pointer was 'lost' when accessing a member function of a class
> through a temporary delegate.
>
> Here is an example:
>
> class A{
> void f () {}
> }
>
> const A a = new A;
> a.f() //error, this is normal.
> auto g = &a.f;
> g(); //no error. Is this normal ?
>
> I may be wrong, but it seems to me that as the const property of the
> this pointer is linked to the instance of the class (here 'a'), the
> delegate 'g' should contain a const pointer to A (because 'a' is const).
>
> More generally, I seems that there is no management of the const
> correctness of the frame pointer of a delegate in D2
>
> What about adding the const keyword for delegates, as we do for methods
> ? The const property of the delegate's frame pointer would be part of
> its type.
>
> Example:
>
> void f()
> {
> int x;
> void g() const {
> x ++; //would produce an error since the type of g is [void
> delegate() const]
> }
> }
>
> Any thoughts about this ?
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
More information about the Digitalmars-d
mailing list