我正在阅读S.Meyers的Effective Modern C++,我发现了一些我无法完全理解的东西。为什么模板内部的类型检查更严格?
第8项解释了为什么nullptr
应该优于0
或NULL
。赞成nullptr
的主要观点是在超载分辨率下更安全的行为。在实践中,你可以避免指针和整数类型之间的无意混淆,但这不是我的问题的关键。
要到我的实际问题时,请考虑下面的代码,这是基于在本书中使用的例子:
#include <memory>
class MyClass {
int a;
};
// dummy functions that take pointer types
int f1(std::shared_ptr<MyClass> spw){return 1;};
double f2(std::unique_ptr<MyClass> upw){return 1.0;};
bool f3(MyClass* pw){return true;};
// template that calls a function with a pointer argument
template<typename FuncType,
typename PtrType>
auto CallFun(FuncType func, PtrType ptr) -> decltype(func(ptr))
{
return func(ptr);
}
int main()
{
// passing null ptr in three different ways
// they all work fine int this case
auto result1 = f1(0); // pass 0 as null ptr to f1
auto result2 = f2(NULL); // pass NULL as null ptr to f2
auto result3 = f3(nullptr); // pass nullptr as null ptr to f3 }
// passing null ptr in three different ways through the template
// only nullptr works in this case
auto result4 = CallFun(f1, 0); // compile error!
auto result5 = CallFun(f2, NULL); // compile error!
auto result6 = CallFun(f3, nullptr); // OK
return 0;
}
前三直接调用f1
,f2
和f3
编译罚款或者0
,NULL
或nullptr
作为空指针。随后的3个调用通过模板函数CallFun
执行更加挑剔:您必须使用nullptr
,否则将不接受整数类型(0
和NULL
)之间的转换。换句话说,类型检查在模板内部发生时似乎更为严格。有人可以澄清发生了什么吗?
我问完全相同的问题:)请参阅我放的dupe链接,并参见标准报价。 – vsoftco