作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?

Member function pointer as template arguement failed on inherited member functions, how and why?

本文关键字:函数 成员 为什么 失败 参数 指针 继承      更新时间:2023-10-16

如下例v0::test(),我们想编写一个模板函数来调用class C的所有成员函数,使用class C的成员函数指针作为模板参数,但这种方式在从参数继承的成员函数上失败了。为了缓解这些故障,我们必须为每个基类添加一个重载(见v1::test()(,在某些更复杂的情况下,甚至很难编写基类名称,例如std:tuple,或者将整个函数设计更改为v2...v4。 有没有更好的方法?

struct A {
void fa() {}
};
struct B : A {
void fb() {}
};
struct C : B {
void fc() {}
};
namespace v0 {
using c_mem_fn = void (C::*)();
template <c_mem_fn fn>
void invoke_c_mem_fn(C* c) {
(c->*fn)();
}
void test() {
C cc;
C* c = &cc;
invoke_c_mem_fn<&C::fc>(c);
//  invoke_c_mem_fn<&B::fb>(c); // compile error
//  invoke_c_mem_fn<&A::fa>(c); // compile error
//  invoke_c_mem_fn<&C::fb>(c); // compile error
//  invoke_c_mem_fn<&C::fa>(c); // compile error
}
} // namespace v0
namespace v1 {
using c_mem_fn = void (C::*)();
template <c_mem_fn fn>
void invoke_c_mem_fn(C* c) {
(c->*fn)();
}
using b_mem_fn = void (B::*)();
template <b_mem_fn fn>
void invoke_c_mem_fn(C* c) {
(c->*fn)();
}
template <void (A::*fn)()>
void invoke_c_mem_fn(C* c) {
(c->*fn)();
}
void test() {
C cc;
C* c = &cc;
invoke_c_mem_fn<&C::fc>(c);
invoke_c_mem_fn<&B::fb>(c);
invoke_c_mem_fn<&A::fa>(c);
invoke_c_mem_fn<&C::fb>(c);
invoke_c_mem_fn<&C::fa>(c);
}
} // namespace v1
namespace v2 {
template <typename Fn, Fn fn>
void invoke_c_mem_fn(C* c) {
(c->*fn)();
}
void test() {
C cc;
C* c = &cc;
invoke_c_mem_fn<decltype(&C::fc), &C::fc>(c);
invoke_c_mem_fn<decltype(&B::fb), &B::fb>(c);
invoke_c_mem_fn<decltype(&A::fa), &A::fa>(c);
invoke_c_mem_fn<decltype(&C::fb), &C::fb>(c);
invoke_c_mem_fn<decltype(&C::fa), &C::fa>(c);
}
} // namespace v2
namespace v3 {
template <typename Fn>
void invoke_c_mem_fn(Fn fn, C* c) {
(c->*fn)();
}
void test() {
C cc;
C* c = &cc;
invoke_c_mem_fn(&C::fc, c);
invoke_c_mem_fn(&B::fb, c);
invoke_c_mem_fn(&A::fa, c);
invoke_c_mem_fn(&C::fb, c);
invoke_c_mem_fn(&C::fa, c);
}
} // namespace v3
namespace v4 {
template <typename X, void (X::*fn)()>
void invoke_c_mem_fn(C* c) {
(c->*fn)();
}
void test() {
C cc;
C* c = &cc;
invoke_c_mem_fn<C, &C::fc>(c);
invoke_c_mem_fn<B, &B::fb>(c);
invoke_c_mem_fn<A, &A::fa>(c);
//  invoke_c_mem_fn<C, &C::fb>(c); // compiler error
//  invoke_c_mem_fn<C, &C::fa>(c); // compiler error
invoke_c_mem_fn<B, &C::fb>(c);
invoke_c_mem_fn<A, &C::fa>(c);
}
} // namespace v4
int main() {
v1::test();
v2::test();
v3::test();
v4::test();
}

我终于在v5::test()中找到了 c++17 语法template <auto>,这解决了我的问题,谢谢大家的阅读和回答。

namespace v5 {
template <auto fn>
void invoke_c_mem_fn(C* c) {
(c->*fn)();
}
void test() {
C cc;
C* c = &cc;
invoke_c_mem_fn<&C::fc>(c);
invoke_c_mem_fn<&B::fb>(c);
invoke_c_mem_fn<&A::fa>(c);
invoke_c_mem_fn<&C::fb>(c);
invoke_c_mem_fn<&C::fa>(c);
}
} // namespace v5