我有一个函数
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 ==========
f(NULL)
可以编译通过吗? - Paul Rr0
。 - Barryf(0)
- 我是在问f(NULL)
,以防万一NULL
被定义为和0
不同的东西。 - Paul R