小编Ser*_*rov的帖子

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

我有一个功能

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, …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

6
推荐指数
1
解决办法
243
查看次数

标签 统计

c++ ×1

c++11 ×1