www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Member function address

reply David Nadlinger <david.nadlinger gmail.com> writes:
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
Nov 21 2009
parent div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLCGLXT9LetA9XoXwRAu7LAJ9GXGar/C/wyjkdRmrYLp5ZHsK7gACeLznU T0RIByTstdeRHgsejvdv+B0= =TgeR -----END PGP SIGNATURE-----
Nov 21 2009