the const correctness of the this pointer

TM ithink at therefore.iam
Mon Dec 28 12:21:46 PST 2009


Jason House Wrote:

> TM Wrote:
> 
> > Jason House 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.
> > 
> > 
> > I disagree with you. To me, the loss of the frame pointer's const attribute looks like a cast. 
> 
> The quoted code (by Steve) above is not the same as yours. Both the variable and the called member are const. Invoking the delegate does not violate the const system. I did respond to your original example saying to file a bugzilla entry.
>  
> 
> 
> > I do not expect to be allowed to call the above 'a.f' wihout casting a into a const A. And yet it is possible by using a delegate, which I think performs a hidden cast operation.
> > 
> 
> Calling a const member function is allowed without casting from either thread-local mutable data or immutable data. Calling a non-const member function on a const object is what's illegal. That's what your original sample did. Constructing such a delegate (without casts) should be illegal and is a bug in dmd.
> 
> Once a const-correct delegate is created, it should be usable like any other delegate.

I totally agree that the problem is the conversion of a [ void delegate() const] to a [void delegate()]. 
I totally agree that we should be allowed to call the const-correct delegate once it has been created.

ie, this should be correct:

void delegate() g = cast ( void delegate() ) (f); //here a cast would be necessary
g(); //This would be OK, of course

Concerning the bugzilla, the problem is that adding the 'const' and 'share' properties of the frame pointer as a part of the delegate's type is more like a new feature than a compiler bug, isn't it ?
On the other hand, it looks like we all agree to say this is a must have feature. Do wee ?

 





More information about the Digitalmars-d mailing list