C语言 C NULL 是否等于 C++11 nullptr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36484473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:23:04  来源:igfitidea点击:

Is C NULL equal to C++11 nullptr

cc++11nullinterop

提问by user877329

I like to use nullptrinstead of NULL. Now I call a C function (from libjanssonin this case).

我喜欢用nullptrNULL 代替。现在我调用一个 C 函数(libjansson在本例中来自)。

NULLin C is implementation defined.

NULL在 C 中是实现定义的

For nullptrI found that "A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero".

因为nullptr我发现“空指针常量是整数类型的整数常量表达式 (5.19) 右值,其计算结果为零”。

So the safest thing to do:

所以最安全的做法是:

auto string_obj=json_object_get(m_handle,name);
if(string_obj!=NULL)
    {
    auto string=json_string_value(string_obj);
    if(string!=NULL)
        {return string;}
    }
return nullptr;

Do I really need that or can I do it simpler:

我真的需要那个还是我可以做得更简单:

auto string_obj=json_object_get(m_handle,name);
if(string_obj!=nullptr)
    {
    return json_string_value(string_obj); //Assume there is no difference between C NULL and C++11 nullptr
    }
return nullptr;

回答by Yakk - Adam Nevraumont

In C++11 and beyond, a pointer that is ==NULLwill also ==nullptrand vice versa.

在 C++11 及更高版本中,也是==NULLwill的指针,==nullptr反之亦然。

Uses of NULLother than comparing with a pointer (like using it to represent the nul byte at the end of a string) won't work with nullptr.

NULL除了与指针进行比较(例如使用它来表示字符串末尾的 nul 字节)之外的其他用途将不适用于nullptr.

In some cases, NULLis #define NULL 0, as the integer constant 0is special-cased in C and C++ when you compare it with pointers. This non-type type information causes some problems in both C and C++, so in C++ they decided to create a special type and value that does the same thing in the "proper" use cases, and reliably fails to compile in most of the "improper" use cases.

在某些情况下,NULLis #define NULL 0,因为0当您将它与指针进行比较时,整数常量在 C 和 C++ 中是特殊的。这种非类型类型信息在 C 和 C++ 中都会引起一些问题,因此在 C++ 中,他们决定创建一个特殊的类型和值,在“适当的”用例中做同样的事情,并且在大多数“不当”的用例。

Insofar as your C++ implementation is compatible with the C implementation you are interoping with (very rare for this not to be true), everything should work.

只要您的 C++ 实现与您正在互操作的 C 实现兼容(非常罕见,因为这不是真的),一切都应该有效。



To be very clear, if ptris any kind of pointer, then the following expressions are equivalent in C++:

非常清楚,如果ptr是任何类型的指针,那么以下表达式在 C++ 中是等效的:

ptr == nullptr
ptr == NULL
ptr == 0
!ptr

As are the following:

如下:

ptr = nullptr
ptr = NULL
ptr = 0

and if Xis some type, so are the following statements:

如果X是某种类型,则以下语句也是:

X* ptr = nullptr;
X* ptr = NULL;
X* ptr = 0;

nullptrdiffers when you pass it to a template function that deduces type (NULLor 0become an intunless passed to an argument expecting a pointer, while nullptrremains a nullptr_t), and when used in some contexts where nullptrwon't compile (like char c = NULL;) (note, not char* c=NULL;)

nullptr当您将它传递给推导类型的模板函数时(NULL0变成一个,int除非传递给一个需要指针的参数,而nullptr仍然是 a nullptr_t),以及在某些nullptr无法编译的上下文中使用时(例如char c = NULL;)(注意,不是char* c=NULL;

Finally, literally:

最后,从字面上看:

NULL == nullptr

is true.

是真的。

The NULLconstant gets promoted to a pointer type, and as a pointer it is a null pointer, which then compares equal to nullptr.

所述NULL常数被提升到指针类型,并且作为一个指针是一个空指针,然后比较等于nullptr



Despite all this, it isn't always true that:

尽管如此,并非总是如此:

 foo(NULL)

and

 foo(nullptr)

do the same thing.

做同样的事。

void bar(int) { std::cout << "int\n"; }
void bar(void*) { std::cout << "void*\n"; }
template<class T>
void foo(T t) { bar(t); }
foo(NULL);
foo(nullptr);

this prints intfor NULLand void*for nullptr.

intNULLvoid*为打印nullptr