Tag Archives: nullptr

nullptr

The nullptr constant is a new addition to C++11 that removes some ambiguity caused by the definition of NULL.

Consider the following code:

#include <iostream>

int f(int)
{
    std::cout << "f(int)" << "\n";
}

int f(void*)
{
    std::cout << "f(void*)" << "\n";
}

int main()
{
    f(NULL); // Ambiguous
    f(0);    // Calls f(int)
}

The first overloaded call is ambiguous because f(NULL) could resolve both to f(void*) and f(int).

The nullptr constant is of type nullptr_t, which can be implicitly converted to void* but the conversion the other way is not allowed.
This allows overloads for both int and void* to work:

int main()
{
    f(nullptr); // Calls f(void*)
    f(0);       // Calls f(int)
}