【发布时间】:2021-05-28 08:28:55
【问题描述】:
在 C++ 中,我正在尝试制作接受第一个参数并在其上调用方法的转发包装器。
这最终出现在给定代码中的 wrapper 方法中。调用它时效果很好。
但是,我想将此模板化包装器分配给函数指针。在我的用例中,函数指针是给定的,我无法将其更改为 std::function 或类似的东西。这是因为它在C api中使用的地方。
我确实创建了以下示例:
#include <iostream>
template<auto FUNC, typename T, typename ... Params>
static auto wrapper(void* handle, Params&& ... args) {
auto* ser = static_cast<T*>(handle);
return (ser->*FUNC)(std::forward<Params>(args)...);
}
class MyTestClass {
public:
int method1(int i, int j) { return i + j; }
float method2(float f) {
return f * 2;
}
};
int main() {
MyTestClass thing{};
int (*serialize)(void* handle, int i, int j);
serialize = wrapper<&MyTestClass::method1, MyTestClass>;
auto callMethod1Result = wrapper<&MyTestClass::method1, MyTestClass>(&thing, 1, 2);
std::cout << "callMethod1Result: " << callMethod1Result << std::endl;
return 0;
}
该方法的调用工作正常,但是:
int (*serialize)(void* handle, int i, int j);
serialize = wrapper<&MyTestClass::method1, MyTestClass>;
不起作用,给我错误:
/.../temp.cpp:23:17: error: no matches converting function ‘wrapper’ to type ‘int (*)(void*, int, int)’
serialize = wrapper<&MyTestClass::method1, MyTestClass>;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/.../temp.cpp:4:13: note: candidate is: ‘template<auto FUNC, class T, class ... Params> auto wrapper(void*, Params&& ...)’
static auto wrapper(void* handle, Params&& ... args) {
^~~~~~~
经过一番尝试,我确实发现 Params&& ... args 部分导致了问题,因为如果我制作一个没有可变参数的更明确的包装器,那么它确实有效。
我的主要问题是:我可以将带有可变参数的模板化方法分配给函数指针吗?如何?
【问题讨论】:
-
C++中没有“模板函数”,有函数模板。
标签: c++ templates function-pointers