【发布时间】:2016-01-22 13:10:06
【问题描述】:
我有以下与 MSVC2013 Update 4 一起使用的类:
template <typename T>
class MyFunction;
template<typename R, class... Ts>
class MyFunction < R(Ts...) >
{
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;
};
如果我这样使用它:
MyFunction<int (int)> f1(nullptr);
MyFunction<int __cdecl(int)> f2(nullptr);
MyFunction<int __stdcall(int)> f3(nullptr);
为什么 f3 编译失败? (考虑到 __cdecl 有效!)。
error C2079: 'f3' uses undefined class 'MyFunction<int (int)>'
error C2440: 'initializing' : cannot convert from 'nullptr' to 'int'
【问题讨论】:
-
nullptr != NULL。想想吧。 -
构造函数接受一个func_type,它是一个函数指针,这样nullptr就可以了吗?
-
我怀疑
R(Ts...)是隐含的R __cdecl (Ts...),所以部分特化不匹配int __stdcall(int)。 -
还要注意 f1 和 f2 可以编译,只有 f3 有问题
-
@T.C.如果是这样,是否有某种方法可以传递调用约定?