在性能方面,使用'nullptr'或仅使用'0'更快吗? [英] Performance wise, is it faster to use 'nullptr' or just '0'?
问题描述
例如:
object* pObject = nullptr;
OR:
object* pObject = 0;
再说一次,哪个是性能更好的明智选择?
Again, which one is better performance wise?
推荐答案
nullptr
的类型为 std :: nullptr_t
,因此构造函数将被重载.这意味着它是null的事实被编码在类型中,因此从理论上讲,可以通过避免条件条件来更快.
nullptr
is of type std::nullptr_t
and so the constructor be overloaded. This means the fact that it is null is encoded in the type, so it can be theoretically faster by avoiding a conditional.
指针显然没有重载的构造函数,因此无论使用 0
还是 nullptr
都是无关紧要的.相同的机器代码,相同的性能.
Pointers obviously don't have overloaded constructors so whether you use 0
or nullptr
is irrelevant. Same machine code, same performance.
无论如何,如果需要空指针,请使用 nullptr
.注意 0
,因为它将调用整数重载而不是指针.
Anyway, go with nullptr
when you want a null pointer. Be careful of 0
as it will call integer overloads rather than pointers.
这篇关于在性能方面,使用'nullptr'或仅使用'0'更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!