the const correctness of the this pointer

TM ithink at therefore.iam
Sun Dec 27 16:02:23 PST 2009


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 ?



More information about the Digitalmars-d mailing list