When MSDN says NULL, is it okay to use nullptr?

In various places, MSDN will talk about the behavior corresponding to the case where a handle type has the value NULL
. A customer wanted to know whether it was safe to use nullptr
in such cases, or whether they have to use NULL

Although the programming languages used by MSDN for documenting Windows are putatively C and C++, MSDN understands that a lot of people write code for Windows in other languages, and therefore it tries to avoid relying on language subtleties

Esoteric definitions for the term NULL
is one of those language subtleties.

Formally, the C and C++ languages permit the following definitions for the NULL

NULL 0 (void*)0 nullptr
Callowedallowednot allowed¹
C++allowednot allowed²allowed

is defined as (void*)0
in C or as nullptr
in C++, then it can be assigned only to a pointer type. And since MSDN cannot control how the C and C++ header files define NULL
, it needs to work with any definition that is permitted by the corresopnding standards. Which means that saying NULL
implies that the underlying type is a pointer type.

Therefore, you are welcome to write nullptr
instead of NULL
if you’re writing C++ code. You’re also welcome to write anything else that produces a null pointer, such as

HMUMBLE h5 = 3 - 3;

But most people would probably prefer you to write NULL
or nullptr

As noted, MSDN understands that a significant portion of its readership is not fluent in the subtleties of C and C++. When it writes NULL
, it means the obvious thing: A null pointer. You can translate that into the appropriate construction for the language you are using. For example, for C#, you can use null
, or if you are operating in raw IntPtr
s, you can use IntPtr.Zero

Bonus chatter
: When MSDN says NULL
, is it okay to use 0
? Yes, but you probably don’t want to. Using 0
as a null pointer constant is permitted by the C and C++ languages for backward compatbility reasons, but it’s not considered modern style.

Bonus bonus chatter
: I’m told that the Visual C++ folks occasionally entertain the possibility of changing the definition of NULL
to nullptr
, which is permitted by the standard. However, this ends up breaking a lot of code which assumed that NULL
is an integral constant evaluating to zero. For example:

void foo(char* p)
  char c = NULL; // would not work if NULL were defined as nullptr
  *p = NULL;     // would not work if NULL were defined as nullptr

Although that code is technically already broken, it manages to work if NULL
is defined as 0
, and updating the definition in the language header files would break existing (albeit poorly-written) code.

¹ C does not have the nullptr

² C++ does not allow NULL
to be defined as (void*)0
because C++ does not permit implicit conversion from void*
to arbitrary T*

int* p = (void*)0; // allowed in C but not C++


Functional programming for embedded systems Functional programming (FP) becomes more and more popular nowadays. A lots of modern languages adopt different concepts of functional programming...
–如何编写so库 开发环境: 硬件:Mac Pro 软件:Android Studio 3.0.1 开发环境:办公室(大弦嘈嘈如急雨,小弦切切如私语) 这次学习编译so库,在网上找了很多教程文章,但都是不成功。最后还是参考官网下的文档成功编译了。遂将此次经历记录下来。 本细节帝主要以本次成功案例为...
答读者问:函数重载 有读者问问题:Rust有没有函数重载? 回答:没有。 本文结束。 ----------------- 开个玩笑。好,下面我们详细说一下重载(overloading)这个话题。 根据 维基百科的定义 ,重载指的是: function overloading ...
浅谈智能指针的历史包袱 我们学习C++的时候,想必都会听说一个叫智能指针的东西,在初听这个词的时候,我们都难免不被“智能”两个字所吸引,感觉它会比较高级,让人有种忍不住想用一把的冲动。 但事实上,它可能并没有我们所想的那样“智能”、好用,它本质上其实就是一个对象,它去接管了原指针所管理的资源。但这里单单用一...
Sliding Responsibility of the Repository Pattern During numerous discussions about the applicability of the Repository pattern, I noticed that people are divided into two groups. In this article, I w...
责编内容来自:The Old New Thing (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » When MSDN says NULL, is it okay to use nullptr?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录