Domanda

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));
È stato utile?

Soluzione

§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)...

Altri suggerimenti

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*.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top