【发布时间】:2019-05-04 21:12:30
【问题描述】:
如何将 const 成员函数 作为 non-const 成员函数 传递给模板?
class TestA
{
public:
void A() {
}
void B() const {
}
};
template<typename T, typename R, typename... Args>
void regFunc(R(T::*func)(Args...))
{}
void test()
{
regFunc(&TestA::A); // OK
regFunc(&TestA::B); // ambiguous
}
不想添加类似:
void regFunc(R(T::*func)(Args...) const)
有没有更好的办法?
【问题讨论】:
-
你需要函数来接受函数指针吗?只需采用通用函数类型或
std::function即可。
标签: c++ class c++11 templates member-function-pointers