C NULL是否等于C ++ 11 nullptr [英] Is C NULL equal to C++11 nullptr

查看:66
本文介绍了C NULL是否等于C ++ 11 nullptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用 nullptr 而不是NULL。现在,我调用一个C函数(在本例中为 libjansson )。

I like to use nullptr instead of NULL. Now I call a C function (from libjansson in this case).

NULL < C中的/ code>是实现已定义

对于 nullptr 我发现空指针常量是整数类型的整数常量表达式(5.19)右值,其值为零 。

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

所以最安全的做法是:

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;


推荐答案

在C ++ 11及更高版本中,是 == NULL 也会是 == nullptr ,反之亦然。

In C++11 and beyond, a pointer that is ==NULL will also ==nullptr and vice versa.

使用 NULL 而不是与指针进行比较(例如使用它来表示字符串末尾的nul字节)不适用于 nullptr

Uses of NULL other 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 #define NULL 0 ,因为整数常量 0 在与指针进行比较时在C和C ++中是特殊情况。这种非类型的信息在C和C ++中都会引起一些问题,因此在C ++中,他们决定创建一个特殊的类型和值,以在适当的用例中执行相同的操作,并且在大多数

In some cases, NULL is #define NULL 0, as the integer constant 0 is 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.

只要您的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.

非常清楚,如果 ptr 是指针,则以下表达式在C ++中是等效的:

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

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

如下所示:

ptr = nullptr
ptr = NULL
ptr = 0

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

and if X is some type, so are the following statements:

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

nullptr 在将其传递给推导类型的模板函数( NULL 0 成为 int 除非传递给期望指针的参数,否则 nullptr 仍为 nullptr_t ),并且在某些情况下使用 nullptr 不会编译(例如 char c = NULL; )(请注意,不是 char * c = NULL;

nullptr differs when you pass it to a template function that deduces type (NULL or 0 become an int unless passed to an argument expecting a pointer, while nullptr remains a nullptr_t), and when used in some contexts where nullptr won't compile (like char c = NULL;) (note, not char* c=NULL;)

最后,从字面上看:

NULL == nullptr

是真的。

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

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

尽管如此,并不总是这样:

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

 foo(NULL)

 foo(nullptr)

做同样的事情。

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);

这会为 NULL void * int $ c>表示 nullptr

this prints int for NULL and void* for nullptr.

这篇关于C NULL是否等于C ++ 11 nullptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆