为什么函数模板不理解NULL但与nullptr一起工作?

Ser*_*rov 6 c++ c++11

我有一个功能

int f(std::shared_ptr<MyClass> sptr);
Run Code Online (Sandbox Code Playgroud)

之后我编写以下模板以便能够调用它(和其他一些)函数:

template <typename Func, typename ArgType>
auto call(Func func, ArgType arg) -> decltype(func(arg))
{
    return func(arg);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试将此模板与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 ==========
Run Code Online (Sandbox Code Playgroud)

Mik*_*our 9

线索在这里:

Arg=int
Run Code Online (Sandbox Code Playgroud)

NULL必须是一个空指针常量,在C++ 11之前,它必须是一个值为零的整数常量.在您的实现中,它有类型int,可能是文字0.

因此,模板参数被推导为int,不可转换为shared_ptr,因此错误.

至于其他人:

auto r0 = f(0); // OK
Run Code Online (Sandbox Code Playgroud)

如果直接传递给函数,则0可以将文字视为空指针常量,并转换为shared_ptr.在其中call,参数不是文字,而是类型的变量,int不能转换.

auto r1 = call(f, nullptr); // OK
Run Code Online (Sandbox Code Playgroud)

nullptr有自己的类型,可以转换为shared_ptr.