是否有任何主要的C ++实现实际上将NULL定义为nullptr? [英] Does any major C++ implementation actually define `NULL` as `nullptr`?
问题描述
自C ++ 11起,标准允许宏NULL
可以是值为零的整数文字或类型为std::nullptr_t
的prvalue.
Since C++11, the Standard allows the macro NULL
to either be a integer literal with value zero, or a prvalue of type std::nullptr_t
.
任何标准库供应商决定将其NULL
的定义从整数更改为nullptr
,很可能会导致依赖C ++ 11之前代码的客户端损坏.
Any Standard Library vendor deciding to change their definition of NULL
from an integer to nullptr
would very likely cause breakage for clients relying on pre-C++11 code.
是否有任何主要的实现方式(例如,GCC,Clang,MSVC)将NULL
定义为nullptr
??或者,尽管有可能,没有人这样做吗?
Does any major implementation (e.g. GCC, Clang, MSVC) actually define NULL
as nullptr
? Or, despite the possibility, does nobody do this?
推荐答案
libstdc++
有关__null
的信息可以在此问题中找到:
Information about __null
can be found in this question:
__null
的实现是作为G ++内部实现的.基本上,内部程序会执行您reinterpret_cast<void *>(0)
天真的期望.
The implementation of
__null
is as a G++ internal. Basically, the internal does what you would naively expectreinterpret_cast<void *>(0)
to do.
其类型为魔术",具体取决于上下文.这就是G ++必须将其作为内部实现的原因.没有常规类型提供精确正确的语义.它的行为大致类似于void *
,但不完全相同.
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++
做几乎相同的事情:
// <cstddef>
//
// Don't include our own <stddef.h>; we don't want to declare ::nullptr_t.
#include_next <stddef.h>
#include <__nullptr>
Microsoft的STL
也依赖于包含stddef.h
:
Microsoft's STL
also relies 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
的定义:
I could not find stddef.h
in the open-source STL repository, but a definition of NULL
is provided in vcruntime.h
:
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
对icc 19.0.1
的简单测试还显示NULL
没有定义为nullptr
:
A simple test on icc 19.0.1
also shows that NULL
is not defined as 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, "");
这篇关于是否有任何主要的C ++实现实际上将NULL定义为nullptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!