尝试这样的:如何创建一个std ::函数一样的包装?
template <class R, class... Ts>
class MyFunction
{
public:
using func_type = R(*)(Ts...);
MyFunction(func_type f)
: m_func(f)
{
}
R operator()(Ts ... args)
{
return m_func(args...);
}
private:
func_type m_func;
};
int Testfn(int a)
{
std::cout << "value is " << a;
return 42;
}
void Testing()
{
MyFunction<int(int)> func(Testfn);
std::cout << "Ret is " << func(1) << std::endl;
}
但失败:
error C2064: term does not evaluate to a function taking 1
C2091: function returns function
C2091: function returns
C2664: 'MyFunction<int (int),>::MyFunction(const MyFunction<int
(int),> &)' : cannot convert argument 1 from 'int (__cdecl *)(int)' to
'int (__cdecl *(__cdecl
*)(void))'
编译器是MSVC2013。
'MyFunction'看起来像它有一个模板参数 - 一个std ::函数。 “MyFunction ”是做什么的? –
doctorlove
编译但我想int(int)风格的语法 – paulm