在C ++中将NULL强制转换为SomeType *的用途是什么? [英] What's the use of casting NULL to SomeType* in C++?

查看:91
本文介绍了在C ++中将NULL强制转换为SomeType *的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一些第三方C ++代码,这些代码对我来说似乎很奇怪(我从C ++ 11开始).让我感到困惑的许多事情之一是从NULL到某种指针类型的static_cast的许多实例:

I'm currently grinding through some third-party C++ code that looks rather odd to me (I started out with C++11). One of the many things that left me puzzled, are the many instances of static_cast from NULL to some pointer type:

SomeClass* someClassPtr = static_cast<SomeClass*>(NULL);

我知道您可以强制转换指针,例如从基类指针到派生类指针,但这里绝对没有继承.据我所知,这应该足够了:

I know you can cast pointers e.g. from a base class pointer into a derived class pointer, but there is absolutely no inheritance going on here. As far as I can see, this should be enough:

SomeClass* someClassPtr = NULL;

但是此代码中唯一没有将NULL强制转换为特定指针类型的情况是向量和其他容器中的指针:

But the only cases in this code, where NULL does not get casted to a specific pointer type, are pointers in vectors and other containers:

SomeOtherClass.vecOfSomeClassPtr[i] = NULL;

所以我的问题是:

  • 这是nullptr之前的简单旧样式(甚至C样式)代码吗?
  • 在处理继承时,是否需要铸造NULL来进行向下/向上转换以外的其他操作?
  • 还是我完全想念什么?
  • Is this simply old-style (or even C-style) code from before there was nullptr?
  • Is/was casting NULL necessary for other things than down-/upcasting when working with inheritance?
  • Or am I missing something completely?

如果到目前为止我没有弄错的话:
我首先用NULL和后来的nullptr替换了static_cast<type*>(NULL)的所有实例,以查看是否会破坏任何内容:不. 编译器没有提出抗议,该程序似乎仍能按预期运行. 但是我知道指针可能是棘手的小杂种,所以:

And in case I haven't gotten it wrong so far:
I first replaced all instances of static_cast<type*>(NULL) with NULL and later nullptr, to see if that would break anything: Nope. The compiler doesn't protest and the program still seems to work as expected. But I know pointers can be tricky little bastards, so:

  • 我可能错过了使用nullptr的哪些陷阱?
  • What pitfalls about the use of nullptr did I probably miss?

PS:是的,我确实使用过搜索,是的,我确实在C代码中找到了类似的问题. 但这是C ++代码,我想肯定知道,而不仅仅是假设.

PS: Yes, I did use the search and yes, I did find similar questions on C code. But this is C++ code and I wanted to know for sure, instead of just assuming something.

推荐答案

在C ++中将NULL强制转换为SomeType *有什么用?

What's the use of casting NULL to SomeType* in C++?

SomeClass* someClassPtr = static_cast<SomeClass*>(NULL);

强制转换在这种情况下不起作用.

The cast has no effect in this context.

但是,更一般而言,在涉及超载的其他某些情况下,它确实有意义:

However more generally, it does make sense in some other context where overloading is involved:

void stupidly_overloaded_function(int);
void stupidly_overloaded_function(SomeClass*);
void stupidly_overloaded_function(SomeOtherClass*);

stupidly_overloaded_function(NULL);

调用哪个函数?它实际上取决于NULL的定义.它要么调用int重载,要么由于模棱两可而导致编译失败.

which function gets called? It actually depends on the definition of NULL. Either it calls the int overload, or compilation fails due to ambiguity.

演员可以消除通话歧义:

A cast can disambiguate the call:

stupidly_overloaded_function(static_cast<SomeClass*>(NULL));

我认为这是引入nullptr的重要原因.尽管即使nullptr也无法处理多个不同的指针重载,但它确实消除了整数之间的歧义:

I think this is a significant reason for introducing nullptr. Although even nullptr cannot deal with multiple different pointer overloads, it does disambiguate between integers:

void not_quite_as_silly_overload(int);
void not_quite_as_silly_overload(SomeClass*);

not_quite_as_silly_overload(NULL);    // might call either overload
not_quite_as_silly_overload(nullptr); // unambiguously calls the pointer overload

C语言中的另一种情况是涉及对象大小的宏,例如Linux内核中的container_of宏:

Another case in C is macros involved with sizes of objects, such as the container_of macro from the Linux kernel:

define container_of(ptr, type, member) ({                      \
       const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
       (type *)( (char *)__mptr - offsetof(type,member) );})

在这里,仅使用0代替NULL.我认为可以在C ++中实现,而无需强制转换,而只能使用C ++ 11中引入的declval.

Here, just 0 is used instead of NULL. I think that this could be implemented in C++ without the cast, but only using declval which was introduced in C++11.

这是nullptr之前的简单旧样式(甚至C样式)代码吗?

Is this simply old-style (or even C-style) code from before there was nullptr?

不,这种多余的演员表没有普遍的风格.您的示例没有重载,因此上述情况不适用于该示例.

No, there was no universal style for such redundant casts. Your example has no overloading, so the above case does not apply to it.

/在处理继承时,是否需要将NULL用于向下/向上转换以外的其他操作?

Is/was casting NULL necessary for other things than down-/upcasting when working with inheritance?

不,它是(并且仍然在不同的指针重载的情况下)用于重载解析的必要条件.

No, it was (and still is in the case of different pointer overloads) necessary for overload resolution.

这篇关于在C ++中将NULL强制转换为SomeType *的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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