30

In C++, it's possible to get a pointer to a (non-static) member function of a class, and then later invoke it on an object. If the function was virtual, the call is dispatched dynamically depending on the dynamic type of the object. It's also possible (not using a member pointer) to call virtual member functions of objects monomorphically, by explicitly providing the scope containing the version to use. The following code demonstrates this:

#include <iostream>
using std::cout; using std::endl;

struct Foo
{
    virtual void foo() { cout << 1 << endl; }
};

struct Foo2: public Foo
{
    virtual void foo() { cout << 2 << endl; }
};

int main( int, char** )
{
    Foo *foo = new Foo2;

    void (Foo::*foo_pointer)() = &Foo::foo;

    foo->foo();            // prints 2
    foo->Foo::foo();       // prints 1
    (foo->*foo_pointer)(); // prints 2
}

What I would like to do is combine the two, and get a pointer to the monomorphic version of a member function; i.e., I want a pointer to Foo::foo which always calls the base class version of foo, and prints 1, even if it is invoked on a Foo2. However, I haven't been able to find a way to do this. Is it possible?

(Other than the tedious manual way of writing a new non-virtual function which makes the monomorphic call, and then getting a pointer to that.)

glaebhoerl
  • 7,695
  • 3
  • 30
  • 41
  • 7
    A new SO user asking a good question with compilable code snippets? Now I don't see that everyday! :-) – In silico Feb 21 '11 at 10:11
  • A pretty tough question even. – Alexandre C. Feb 21 '11 at 10:17
  • 1
    I wonder why you would want to do this, and whether there isn't a better way to achieve whatever you want to achieve with that. (Oh, and I don't think this is possible, but C++ keeps surprising me, so I won't blurt out with an answer saying so.) – sbi Feb 21 '11 at 10:23
  • @sbi I'm working on language bindings, and have a struct of function pointers corresponding to the virtual functions of a class (effectively a second vtable). The class itself gets subclassed so that each virtual function calls out to this 'vtable'. In the case where a virtual function hasn't been overriden from the other language, I want the entry to point back to the original version of the function -- but it has to be the monomorphic version, otherwise the only result is an infinite loop (and, er, a stack overflow). – glaebhoerl Feb 21 '11 at 10:30
  • 3
    @illissius: have you considered using lambdas or `std::function` to do that? GCC and VC10 support these. – sbi Feb 21 '11 at 10:34
  • @sbi: hmm, that's a nice idea -- if it can't be done directly, that's still definitely nicer than writing a whole new 'top-level' function to do it manually. thanks. – glaebhoerl Feb 21 '11 at 10:45
  • @In silico: because most people capable of asking a good question with compilable code snippets are already SO members! – Steve Jessop Feb 21 '11 at 11:58
  • 1
    @illissius: How would you realize whether a virtual function has or hasn't been overriden from the other language? – ali_bahoo Feb 21 '11 at 12:10
  • @sad_man: well, that's the responsibility of the other language :). (for the record, the "other language" is Haskell, and the answer at present is either "the user says so" or "evil yet effective hacks.") – glaebhoerl Feb 21 '11 at 12:33

3 Answers3

12

It's possible in GCC, but the way it's documented in C++ language extensions section suggests there's no portable way to do it.

You can do two things:

  1. If you control the class, create a non-virtual function and a virtual wrapper for it and when you know you don't need virtual dispatch, just take address of the non-virtual one.
  2. If you don't, create a template functor that will hold the member pointer and do the explicit scope call.
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • 1
    Bound function pointers (commonly called delegates) are something else entirely. – Ben Voigt Feb 21 '11 at 15:48
  • 1
    Nah, this seems to be exactly what I was looking for (and agree that if it's a GCC extension there's probably no standards-conformant way to do it). It's nothing to do with delegates; it converts a void (Foo::*)() which goes through the vtable to a void (\*)(Foo*) which doesn't. Which, again, is exactly what I wanted. Thanks a lot! – glaebhoerl Feb 22 '11 at 11:10
  • 1
    @Jan Hudec "If you don't, create a template functor that will hold the member pointer and do the explicit scope call."? How to understand that in the right way?Could you please give me a simple code snippet? – John Aug 15 '21 at 07:22
  • Joining in @John – I've been failing to create such a template myself (`static_cast` to base creating a temporary copy broke my attempts...) – so would be really curious how that would look like! (And yes, I know I'm pretty late – have been failing even with more recent C++ standard...). – Aconcagua Feb 04 '22 at 11:59
  • Needed to discover that the GCC extension needed a static cast to base – not reference or pointer to – to actually call the base function; pointer or reference didn't put to ground virtuality :( – Aconcagua Feb 04 '22 at 13:16
0

To elaborate with a code example for a wrapper function (and despite the fact the OP wanted to avoid this method!) as in many cases this is the pragmatically preferable solution:

#include <iostream>
using std::cout; using std::endl;

struct Foo
{
    virtual void foo() { cout << 1 << endl; }
};

struct Foo2: public Foo
{
    virtual void foo() { cout << 2 << endl; }
};

void monomorphicFooFoo( Foo * f ) { f->Foo::foo(); }

int main()
{
    Foo *foo = new Foo2;

    void (*baseFoo)( Foo * ) = &monomorphicFooFoo;
    baseFoo( foo ); // Prints 1
}
SimonD
  • 638
  • 5
  • 16
0

In other words : you want to cheat.

No, it is not possible because that is how the polymorphism in combination with pointer to member method works.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • 1
    that *is* the way polymorphism works. As the questioner demonstrates, it is possible to make the desired "cheating" call, with the syntax `foo->Foo::foo();`. It merely isn't the way that member function pointers work. – Steve Jessop Feb 21 '11 at 11:56
  • @Steve You are right. I will modify my answer. But the real answer is the combination of two : that is how the polymorphism in combination with pointer to member methods works. – BЈовић Feb 21 '11 at 15:07