Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c++ - Can the NULL macro actually be a nullptr?

According to the draft of the standard N4713 (7.11/1):

A null pointer constant is an integer literal (5.13.2) with value zero or a prvalue of type std::nullptr_t.

and 21.2.3/2:

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

follow that NULL can be defined as nullptr. Same is mentioned on cppreference:

#define NULL 0
//since C++11
#define NULL nullptr

At the same time "Additive operators" clause says (8.5.6/7):

If the value 0 is added to or subtracted from a null pointer value, the result is a null pointer value. If two null pointer values are subtracted, the result compares equal to the value 0 converted to the type std::ptrdiff_t.

Hence the following code should be valid:

0 + nullptr; 
nullptr - nullptr; 

but because of the lack of +/- operators for std::nullptr_t the code is invalid.

Is there something that I didn't take into account or NULL macro can't be actually defined as nullptr?

question from:https://stackoverflow.com/questions/48904015/can-the-null-macro-actually-be-a-nullptr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While nullptr is a null pointer constant, it is not a null pointer value. The latter is a value of some pointer type, which std::nullptr_t is not.

Reference:

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. Such a conversion is called a null pointer conversion. [...]

7.11/1 in N4659, emphasize mine

So NULL can indeed be nullptr without providing the arithmetic operators.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...