刚刚看了一篇关于C++0x标准的文章:http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-why-you-should-care/
它说nullptr
是强类型的,这意味着它可以与整数 0 区分开来。
f(int);
f(char* p);
f(nullptr); // calls char* version
一切都很好,但我很想知道标准对
nullptr
的看法有两个指针函数:f(char* p);
f(int* p);
f(nullptr);
我这里需要 Actor 吗?并且是
nullptr
模板化?f((int*)(nullptr);
f(static_cast<int*>(nullptr));
f(nullptr<int>); // I would prefer this
最佳答案
我还没有阅读有关此规范的实际规范,但我很确定您指出的调用在没有强制转换的情况下会不明确,因为空指针可以转换为任何类型的指针。所以 Actor 阵容应该是必要的。
不,不幸的是,nullptr
不是模板。不过,我真的很喜欢这个想法,所以你可以考虑编写一个这样的函数:
template <typename PtrType> PtrType null() {
return static_cast<PtrType>(nullptr);
}
然后你可以写
f(null<int*>());
关于types - nullptr 的强类型化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6430693/