作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?
Member function pointer as template arguement failed on inherited member functions, how and why?
如下例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
相关文章:
- 如何使用指针传递给函数的数组中对象的函数成员
- c++构造函数成员初始化:传递参数
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何在C++通过公共函数访问私有函数成员?
- 解释了构造函数成员初始化列表
- 调用std::函数成员时内存损坏
- 是否可以为模板类的模板函数成员设置别名?
- 捕获 lambda 函数C++成员变量
- 构造函数成员初始值设定项跨成员列出,安全吗?
- 获取与在模板参数中传递的函数成员类型相同的类
- 如何从公共函数成员访问地图私有成员
- C 构造函数成员分配优化
- 使用命名空间进行函数成员定义
- 函数成员作为 CUDA 内核的参数
- 模板基类函数成员的别名
- 函数成员中用于void和继承的enable_if
- 头文件中是否定义了一个很长的Class函数成员
- 类内/构造函数成员初始化
- 使用指向部分专用函数成员的指针自动填充向量
- 指向函数成员的指针