5

I'm trying to directly call a virtual method passed to a template, however, it's getting turned into a virtual call

class Test {
public:
    virtual void func() {
        printf("foo\n");
    }
};

template <auto F, typename T>
void foo(T* instance) {
    (instance->*F)();
}

int main() {
    auto a = new Test;

    a->Test::func(); // directly calls it

    auto f = &Test::func;

    (a->*f)(); // virtual call

    foo<&Test::func>(a); // virtual call
}

The intention is for foo<&Test::func>(a) to do the same as a->Test::func()

godbolt link

mat
  • 314
  • 1
  • 3
  • 9

0 Answers0