C++ Quiz161 of 173
Difficulty:
⬅️ Prev: 334Quiz ID: 335Next: 337 ➡️

According to the C++23 standard, what is the output of this program?

#include <cstddef>
#include <iostream>

void f(void*) {
    std::cout << 1;
}

void f(std::nullptr_t) {
    std::cout << 2;
}

int main() {
    int* a{};

    f(a);
    f(nullptr);
    f(NULL);
}
Hint

NULL is not necessarily an integer literal.

Reveal answer

The program is unspecified / implementation defined

Explanation

§[support.types.nullptr]¶2:

The macro NULL is an implementation-defined null pointer constant.

§[conv.ptr]¶1:

A null pointer constant is an integer literal (§[lex.icon]) with value zero or a prvalue of type std::nullptr_t.

In most implementations, NULL will be an integer literal and the program will not compile due to ambiguous call f(NULL). However, the C++ standard allows NULL to be a prvalue of std::nullptr_t in which case the program would output 122.

Here is an example where GCC and Clang use long for NULL, and MSVC uses int.

⬅️ Prev: 334Quiz ID: 335Next: 337 ➡️