是否有任何主要的 C++ 实现实际上将 `NULL` 定义为 `nullptr`?

Does any major C++ implementation actually define `NULL` as `nullptr`?

自 C++11 起,标准允许宏 NULL 为值为零的整数文字,或类型为 std::nullptr_t 的纯右值。

任何决定将 NULL 的定义从整数更改为 nullptr 的标准库供应商很可能会导致依赖于 C++11 之前代码的客户端出现故障。

是否有任何主要实现(例如 GCC、Clang、MSVC)实际上将 NULL 定义为 nullptr 或者,尽管有可能,但没有人这样做这个?

libstdc++ relies on including stddef.h,这样定义NULL

// <stddef.h>
//
#if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL     /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else   /* G++ */
#ifndef __cplusplus
#define NULL ((void *)0)
#else   /* C++ */
#define NULL 0
#endif  /* C++ */
#endif  /* G++ */
#endif  /* NULL not defined and <stddef.h> or need NULL.  */
#undef  __need_NULL

有关 __null 的信息可以在 this question:

中找到

The implementation of __null is as a G++ internal. Basically, the internal does what you would naively expect reinterpret_cast<void *>(0) to do.

Its type is 'magic', depending on context. That's the reason G++ had to implement it as an internal. No regular type provides the precisely-correct semantics. It acts roughly like void *, but not exactly.


libc++ does pretty much the same thing:

// <cstddef>
//
// Don't include our own <stddef.h>; we don't want to declare ::nullptr_t.
#include_next <stddef.h>
#include <__nullptr>

微软的 STLrelies on including stddef.h:

#pragma once
#ifndef _CSTDDEF_
#define _CSTDDEF_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR

#include <stddef.h>

我在开源 STL 存储库中找不到 stddef.h,但 vcruntime.h 中提供了 NULL 的定义:

#ifndef NULL
    #ifdef __cplusplus
        #define NULL 0
    #else
        #define NULL ((void *)0)
    #endif
#endif

icc 19.0.1 的简单测试也表明 NULL 未定义为 nullptr:

#include <type_traits>
#include <cstddef>

static_assert(!std::is_same<decltype(NULL), std::nullptr_t>::value, "");
static_assert(!std::is_same<decltype(NULL), int>::value, "");
static_assert(std::is_same<decltype(NULL), long>::value, "");

live on godbolt.org