skip to content

In C++, with overloads f(int) and f(char*), what do the calls f(0), f(nullptr) and f(NULL) each resolve to?

level: middleimportance: must knowfreq 65%

answer

  1. zero is an int first
  2. nullptr has its own type
  3. NULL is implementation-defined
  4. __null on clang and GCC

basics

~10 s

f(0) calls f(int), an exact match. f(nullptr) calls f(char*), because std::nullptr_t never converts to int. f(NULL) depends on the implementation's NULL: defined as 0 it calls f(int); clang's __null makes the call ambiguous.

solid answer

~50 s

The literal `0` has type `int`, so `f(int)` is an exact match; `0` is also a null pointer constant, but reaching `char*` takes a pointer conversion, which ranks lower. `nullptr` has type `std::nullptr_t`, which converts to any pointer type but not to `int`, so only `f(char*)` is viable. `NULL` is the problem child: the standard says only that it is an implementation-defined null pointer constant (`0` and `0L` are allowed, `(void*)0` is not). If it expands to `0`, `f(NULL)` silently calls the *integer* overload. Clang and GCC expand it to the builtin `__null`, a pointer-sized null pointer constant (of type `long` on clang for 64-bit macOS), and with clang the call is rejected as ambiguous. The fix is to pass `nullptr`, and, for an API, to make sure a pointer overload is never competing with an integer one on `0`.

code

cpp · 13 lines
cpp
#include <cstddef>
#include <cstdio>

void f(int)   { std::puts("f(int)"); }
void f(char*) { std::puts("f(char*)"); }

int main() {
    f(0);          // 0 is an int: exact match            -> f(int)
    f(nullptr);    // std::nullptr_t -> char*: only option  -> f(char*)
#ifdef TRY_NULL
    f(NULL);       // clang + libc++: error, call to 'f' is ambiguous
#endif
}

go deeper

for a junior

Remember that 0 is an int first, that nullptr is the null pointer to write in modern C++, and that f(nullptr) picks the pointer overload.

for a middle

Explain the ranking behind each call, why NULL is implementation-defined, and why clang's __null makes f(NULL) ambiguous rather than calling either overload.

for a senior

Show how an int-versus-pointer overload pair invites silent wrong calls in callers' code, and how a std::nullptr_t overload or a different API shape removes the trap.

for a principal

Decide whether a codebase enforces nullptr through a warning or tool in the build, and how to migrate legacy NULL uses without changing which overloads run.

## Three spellings of "no pointer" C++ code meets three ways of writing a null pointer, and they behave very differently in overload resolution: - **`0`**: an integer literal of type `int`. It is also a **null pointer constant**, which means it *can* be converted to any pointer type. - **`NULL`**: a macro from `<cstddef>` (and other headers). The standard requires only that it be an **implementation-defined null pointer constant**; its footnote names `0` and `0L` as possible definitions and rules out `(void*)0`. - **`nullptr`** (C++11): a prvalue of type **`std::nullptr_t`**. It converts implicitly to every object pointer, function pointer and pointer-to-member type, and it does not convert implicitly to `int`. The current wording defines a null pointer constant as "an integer literal with value zero or a prvalue of type `std::nullptr_t`". An `int` variable that happens to hold zero is not one. ## How each call is ranked With `void f(int)` and `void f(char*)`: | Call | Conversion to `int` | Conversion to `char*` | Result | |---|---|---|---| | `f(0)` | Exact Match | null pointer conversion (Conversion rank) | `f(int)` | | `f(nullptr)` | not viable | null pointer conversion | `f(char*)` | | `f(NULL)`, `NULL` is `0` | Exact Match | Conversion | `f(int)` | | `f(NULL)`, `NULL` is a `long`-typed constant | integral conversion (Conversion) | Conversion | ambiguous | The last row is what clang does. Apple clang with libc++ expands `NULL` to `__null`; on 64-bit macOS `decltype(NULL)` is `long`. Converting `long` to `int` is an integral conversion and converting the null pointer constant to `char*` is a pointer conversion; both are Conversion rank, so the compiler reports `call to 'f' is ambiguous`. GCC's own documentation also says it defines `NULL` as `__null`, a null pointer constant guaranteed to be the same size as a pointer. ## Why NULL is the dangerous one The trouble is not the ambiguity, which at least stops the build. It is the **silent integer overload**: 1. On an implementation where `NULL` is plain `0`, `f(NULL)` compiles and calls `f(int)`, the opposite of what the author meant. 2. On clang, if the overload set contains a `long` overload, `NULL` picks it as an exact match: with `print(int)`, `print(long)`, `print(double)` and `print(const char*)`, the call `print(NULL)` runs `print(long)`. 3. Moving the same code to another compiler can change which function runs, because the type of `NULL` is the implementation's choice. `nullptr` removes all three problems because its type is not an integer at all. ## What to do in real code - Write **`nullptr`** for every null pointer in C++11 and later; treat `NULL` and a literal `0` used as a pointer as review findings. - When an API must accept "no value" through an overload, give it a **`std::nullptr_t` overload** (or `std::optional`, covered with vocabulary types) so the intent is explicit: `f(nullptr)` then selects that overload as an exact match. - Avoid overload sets where an integer parameter and a pointer parameter sit in the same position; callers passing `0` hit the integer one, or an ambiguity when the integer parameter is not `int`. - Clang's `-Wzero-as-null-pointer-constant` flags a literal `0` used as a pointer; it stays silent on `NULL`, which clang expands to `__null`, so a migration also needs a search for the macro. ## Zero stops being null when it is forwarded A literal `0` is a null pointer constant only while it is a literal. Pass it through a template and it becomes an ordinary `int`: ```cpp void g(char*); template <class T> void call_g(T arg) { g(arg); } call_g(nullptr); // OK: T is std::nullptr_t, which converts to char* call_g(0); // error: T is int, and an int variable is not a null pointer constant ``` The same happens in every forwarding wrapper, factory and `emplace`-style function, which is one more reason `nullptr` is the only spelling that survives being passed along. ## The interview point This question is the canonical answer to "why was `nullptr` added?". The short form: `0` and `NULL` are integers that the language lets you use as pointers, so overload resolution treats them as integers first. `nullptr` has a type of its own, so overload resolution can tell a null pointer from the number zero.

  • How would you make f(nullptr) reach a dedicated overload?
    Declare `void f(std::nullptr_t)` from `<cstddef>`. For a `nullptr` argument that overload is an exact match, while `f(char*)` needs a null pointer conversion, so the dedicated one wins. It is a common way to give "no object" its own behaviour without a sentinel value.
  • Is an int variable holding zero a null pointer constant?
    No. Since C++11 (the standard lists it as a change from C++03) the definition is an integer *literal* with value zero, or a prvalue of type `std::nullptr_t`. `int z = 0; char* p = z;` is ill-formed, and `f(z)` with `f(int)` and `f(char*)` simply calls `f(int)`.

  • NULL may expand to (void*)0 in C++, as it does in many C libraries
  • f(0) prefers the pointer overload because 0 means null
  • nullptr converts to int as zero when no pointer overload exists
  • NULL behaves the same on every compiler, so it is as safe as nullptr
  • An int variable equal to zero converts implicitly to a null pointer