November 21, 2009 Member function address | ||||
---|---|---|---|---|
| ||||
In a wrapper generator I am writing, I need to check if some member function foo() has been overridden in a subclass. Thanks to the help of maxter at #d, I currently have this for D1: template addressOf( alias fn ) { const addressOf = &fn; } class A { void foo() {} final void bar() { auto dg = &foo; if ( addressOf!( foo ) == dg.funcptr ) { Stdout( "not overridden" ).newline; } else { Stdout( "overridden" ).newline; } } } class B : A { override void foo() {} } void main() { A a = new A(); a.bar; A b = new B(); b.bar; } Although the code works fine with DMD, LDC complains that »this« is needed to evaluate &foo in addressOf. Is this a bug in LDC or in fact illegal code which DMD happens to erroneously accept? Are there any less hackish ways to perform the check? Thanks, klickverbot |
November 21, 2009 Re: Member function address | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger Attachments: | David Nadlinger wrote: >> Although the code works fine with DMD, LDC complains that »this« is needed > to evaluate &foo in addressOf. That looks like a ldc bug. &foo is occurring in a normal method so it does have 'this'. > Is this a bug in LDC or in fact illegal code > which DMD happens to erroneously accept? Are there any less hackish ways to > perform the check? Not with D1 I think. D2 has traits to help with this sort of thing. http://www.digitalmars.com/d/2.0/traits.html - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk |
Copyright © 1999-2021 by the D Language Foundation