What is the difference between a null pointer constant (nullptr), a null pointer value and a null member pointer value?
Asked Answered
U

1

8

In ISO/IEC 14882:2017 (C++17), Section 5.13.7 "Pointer literals" is stated:

5.13.7 Pointer literals [lex.nullptr]

pointer-literal: nullptr

1 The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 7.11 and 7.12. —end note]

Following, nullptr is a prvalue of type std::nullptr_t. A prvalue of type std::nullptr_tis a null pointer constant; thus nullptr is a null pointer constant. A null pointer constant (so is nullptr) can be converted to a null pointer value or a null member pointer value.

Now I have in the cited Section 7.11, "Pointer conversions" of ISO/IEC 14882:2017:

A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.

I understand that a null pointer constant is an integer literal with a value of zero or a prvalue of type std::nullptr_t, but I do not understand the difference to the null pointer value nor the null member pointer value. I do not understand how the result of the conversion from a null pointer constant to a pointer type is a "null pointer value of that type" and what a null pointer value in that context is.

My Question is:

  • What is the difference between a null pointer constant (which is nullptr), a null pointer value and a null member pointer value?
Uncontrollable answered 30/1, 2020 at 16:13 Comment(12)
Have you seen 7.11 and 7.12 you are referred to? There's a formal definition of the terms "null pointer value" and "null pointer constant" there. TLDR; An integer variable may have a value 0. That value may be syntactically represented in different ways - e.g. 0, 0x0, 1-1; these are constants, or constant expressions, having a value 0. Similarly, each pointer type has a null value. There are many different ways to lexically represent a null pointer value in the text of the program - e.g. nullptr or 0. These ways are collectively called "null pointer constant".Ostium
@FrançoisAndrieux Yes.Uncontrollable
@IgorTandetnik What I understand is: "A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t. ". but not "A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type."Uncontrollable
Which part of this second sentence is unclear?Ostium
@IgorTandetnik I do not understand how the result of the conversion from a null pointer constant to a pointer type is a "null pointer value of that type" and what a null pointer value in that context is.Uncontrollable
Every type has a set of values. Every pointer type has one special value in that set, distinct from others, known as "null pointer value". This value has certain properties that other values do not; e.g. a pointer having this value is known not to point to any valid object; dereferencing such a pointer exhibits undefined behavior. Now, it's useful to have a way to express this value in the text of the program. To this end, any syntactical construct that satisfies the requirements of being a "null pointer constant", when converted to a pointer type, produces a null pointer value of that type.Ostium
@IgorTandetnik This makes it clearer to me, TYVM. Has this null pointer value a certain denotation like a macro for it, if I want to proof a pointer for that value? For example something like the NULL macro -> if(ptr == NULL){...}? And how does that value differ from the value stored in/for nullptr?Uncontrollable
@RobertSsupportsMonicaCellio nullptr is a keyword. It is not an object, so nothing is "stored" in it.Bowrah
@Bowrah So, (int*)nullptr denotes the null pointer value for an object of type int*?Uncontrollable
@RobertSsupportsMonicaCellio Yes.Bowrah
NULL is a macro that, in many implementations, simply expands to 0. There's nothing magical in it. Something like this would likely compile: int x = NULL; That's pretty much why nullptr was invented - int x = nullptr; does not compile, so using nullptr instead of NULL prevents some nonsense code.Ostium
@IgorTandetnik Some nonsense I have faced in Can I use NULL as substitution for the value of 0? for example?Uncontrollable
B
9

What is the difference between a null pointer constant (nullptr), a null pointer value and a null member pointer value?

Null pointer constant is either nullptr (or any other prvalue of type std::nullptr_t), or integer literal of value 0. Examples of null pointer constants:

NULL  // a macro for one of the others
0
0L
nullptr

std::nullptr_t fun();
fun() // also a null pointer constant

Of these, don't use the integer literals nor NULL unless you need to support older than C++11 standard.

Null pointer value is the value of a pointer type that represents null. Examples of null pointer values:

(void*)nullptr     // pointer to void
(int*)nullptr      // pointer to object

using void_fun = void();
(void_fun*)nullptr // pointer to function

Null member pointer value is the value of a member pointer type that represents null. Example of null member pointer values:

(int some_class::*)nullptr // pointer to data member

using mem_void_fun_ptr = void(some_class::*)(); // unfortunately cannot alias member
                                                // function type as far as I know,
                                                // so we alias pointer to it instead
(mem_void_fun_ptr)nullptr  // pointer to member function

Note that std::nullptr_t is not a pointer type nor a pointer to member type.

Bowrah answered 30/1, 2020 at 16:22 Comment(1)
Maybe add that std::nullptr_t is sometimes useful in overloading functions, to get a function that gets called specifically for handling the case of a null pointer. But, all in all, nice answer. +1.Recording

© 2022 - 2025 — McMap. All rights reserved.