HiveBrain v1.2.0
Get Started
← Back to all entries
principlecppCritical

NULL vs nullptr (Why was it replaced?)

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
nullptrwhyreplacednullwas

Problem

I know that in C++ 0x or NULL was replaced by nullptr in pointer-based applications. I'm just curious of the exact reason why they made this replacement?

In what scenario is using nullptr over NULL beneficial when dealing with pointers?

Solution

nullptr has type std::nullptr_t. It's implicitly convertible to any pointer type. Thus, it'll match std::nullptr_t or pointer types in overload resolution, but not other types such as int.

0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things:

f(int);
f(foo *);


(Thanks to Caleth pointing this out in the comments.)

Code Snippets

f(int);
f(foo *);

Context

Stack Overflow Q#20509734, score: 506

Revisions (0)

No revisions yet.