我有一个功能
int f(std::shared_ptr<MyClass> sptr);
之后我编写以下模板以便能够调用它(和其他一些)函数:
template <typename Func,typename ArgType>
auto call(Func func,ArgType arg) -> decltype(func(arg))
{
return func(arg);
}
当我尝试将此模板与NULL一起使用时,为什么在第三行中出现错误?
auto r0 = f(0); // OK auto r1 = call(f,nullptr); // OK auto r2 = call(f,NULL); // ERROR! WHY?? 1>------ Build started: Project: ConsoleApplication1,Configuration: Debug x64 ------ 1> main.cpp 1>main.cpp(245): error C2893: Failed to specialize function template 'unkNown-type call(Func,Arg)' 1> With the following template arguments: 1> 'Func=int (__cdecl *)(std::shared_ptr<MyClass>)' 1> 'Arg=int' ========== Build: 0 succeeded,1 Failed,0 up-to-date,0 skipped ==========
解决方法
线索在这里:
Arg=int
NULL必须是空指针常量,在C 11之前,它必须是一个值为零的整数常量.在您的实现中,它具有int类型,可能是文字0.
因此模板参数推导为int,不能转换为shared_ptr,因此错误.
至于其他人:
auto r0 = f(0); // OK
如果直接传递给函数,则可以将文字0视为空指针常量,并将其转换为shared_ptr.在调用中,参数不是文字,而是int类型的变量,无法转换.
auto r1 = call(f,nullptr); // OK
nullptr有自己的类型,可以转换为shared_ptr.