模板化方法指针 - 无法匹配函数参数的指针
Templated method pointer - can't match pointer for function argument
我正在制作一个这样的方法指针包装器:
template<typename OBJECT, typename... ARGS>
method_wrapper<ARGS...> _getWrapper(OBJECT* object, void (OBJECT::*method)(ARGS...))
{
//irrelevant
}
问题就在_getWrapper
的召唤下:
class TestClass
{
void TestMethod(int a, float b, bool c)
{
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
std::cout<<c<<std::endl;
}
};
int main()
{
TestClass testObj;
method_wrapper<int, float, bool> wrap = _getWrapper<int, float, bool>(&testObj, TestClass::TestMethod);
wrap.callInternal(1000, 3.14, true);
//...
system("pause");
return 0;
}
无论我试图以何种方式传递_getWrapper中的论点,它仍然告诉我:
没有与参数列表匹配的重载函数实例
OBJECT::*method
不是直接匹配TestClass::TestMethod
吗?我也试过&TestClass::TestMethod
,也不匹配。
您在调用_getWrapper
时显式指定了模板参数,并且第一个参数被指定为模板参数OBJECT
的int
,这是错误的。因为成员指针不能引用非类类型。
改变
_getWrapper<int, float, bool>(&testObj, TestClass::TestMethod)
自
_getWrapper<TestClass, int, float, bool>(&testObj, &TestClass::TestMethod)
// ~~~~~~~~~~
请注意,您可以仅依靠模板类型推断,例如
_getWrapper(&testObj, &TestClass::TestMethod)
顺便说一句:为了从成员那里获取地址,您应该始终使用&
。
顺便说一句:我想TestClass::TestMethod
是public
.
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址