C++ 是否有任何主要的C++;实现是否将“NULL”定义为“nullptr”?
自C++11以来,该标准允许宏C++ 是否有任何主要的C++;实现是否将“NULL”定义为“nullptr”?,c++,c++11,null,c++-standard-library,nullptr,C++,C++11,Null,C++ Standard Library,Nullptr,自C++11以来,该标准允许宏NULL要么是值为零的整数文本,要么是std::nullptr\t类型的prvalue 任何标准库供应商决定将其NULL的定义从整数更改为nullptr,这很可能会导致依赖C++11之前版本代码的客户中断 是否有任何主要实现(如GCC、Clang、MSVC)实际将NULL定义为nullptr?或者,尽管可能,没有人这样做?libstdc++,它以以下方式定义NULL: // <stddef.h> // #if defined (_STDDEF_H) ||
NULL
要么是值为零的整数文本,要么是std::nullptr\t
类型的prvalue
任何标准库供应商决定将其NULL
的定义从整数更改为nullptr
,这很可能会导致依赖C++11之前版本代码的客户中断
是否有任何主要实现(如GCC、Clang、MSVC)实际将NULL
定义为nullptr
?或者,尽管可能,没有人这样做?libstdc++
,它以以下方式定义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
微软的
STL
也:
对
ICC19.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, "");
#包括
#包括
静态断言(!std::is_same::value,“”);
静态断言(!std::is_same::value,“”);
静态断言(std::is_same::value,“”);
您可以为您的工具链确定这一点(事实上,这三个工具链都是如此)。您在获取此信息时是否遇到了一些实际问题?如果将
NULL
从0L
更改为NULL ptr
会破坏您的代码,那么您的代码总是会被破坏。您所说的“与C共享”是什么意思?对于C++和#pragma once
#ifndef _CSTDDEF_
#define _CSTDDEF_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <stddef.h>
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
#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, "");