[英]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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.