最近在研究 unique_ptr 的实现。其中需要分辨出函数类型和其他类型。 实验发现 MSVC 的编译器似乎无法正确的处理函数调用方式。
template <typename R>
struct is_unary_function_impl<R(__cdecl*)()>
{
static const bool value = true;
};
template <typename R>
struct is_unary_function_impl<R(__cdecl*)(...)>
{
static const bool value = true;
};
bool _cdecl unary_func_1() {
return true;
}
bool _fastcall unary_func_2() {
return true;
}
int main()
{
ASSERT(true == is_unary_function<decltype(&unary_func_1)>::value);
//现在 value == false,它应该为 true。
ASSERT(true == is_unary_function<decltype(&unary_func_2)>::value);
return 0;
}
如果在模板声明后面加上这样的代码:
template <typename R>
struct is_unary_function_impl<R(__stdcall*)()>
{
static const bool value = true;
};
template <typename R>
struct is_unary_function_impl<R(__stdcall*)(...)>
{
static const bool value = true;
};
编译器会给出一个编译错误
error C2953: 'is_unary_function_impl<R(__cdecl *)(void)>': class template has already been defined
我要怎么改才能让编译器能识别_stdcall 的函数?