20

Can I use the nullptr keyword as an argument for a variable function? If so, does it undergo any kind of standard conversion, and what is the type of the resulting value?

Concretely, is the following correct?

std::printf("%p", nullptr);

Or does it have to be:

std::printf("%p", static_cast<void *>(nullptr));
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084

2 Answers2

22

§5.2.2p7 When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg (18.10)... An argument that has (possibly cv-qualified) type std::nullptr_t is converted to type void* (4.10)...

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
11

The standard says that any argument of type nullptr_t will be converted to void* when matching .... So the call is correct without the cast.

EDIT:

From the standard (§5.2.2/7):

When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg. The lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversions are performed on the argument expression. An argument that has (possibly cv-qualified) type std::nullptr_t is converted to type void*.

James Kanze
  • 150,581
  • 18
  • 184
  • 329