奇怪的 static_cast 技巧?
在仔细阅读 Qt 源代码时,我遇到了这个 gem:
template <class T> inline T qgraphicsitem_cast(const QGraphicsItem *item)
{
return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}
注意到 static_cast
?我使用 C++ 已经很多年了,但以前从未见过在 static_cast 中使用 0 。这段代码在做什么并且安全吗?
背景:如果您从 QGraphicsItem
派生,则需要声明一个名为 Type
的唯一枚举值,并实现一个名为 type
的虚拟函数来返回它,例如:
class Item : public QGraphicsItem
{
public:
enum { Type = MAGIC_NUMBER };
int type() const { return Type; }
...
};
然后您可以这样做:
QGraphicsItem* item = new Item;
...
Item* derivedItem = qgraphicsitem_cast<Item*>(item);
这可能有助于解释 static_cast 试图做什么。
While perusing the Qt source code I came across this gem:
template <class T> inline T qgraphicsitem_cast(const QGraphicsItem *item)
{
return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}
Notice the static_cast<T>(0)->Type
? I've been using C++ for many years but have never seen 0 being used in a static_cast before. What is this code doing and is it safe?
Background: If you derive from QGraphicsItem
you are meant to declare an unique enum value called Type
that and implement a virtual function called type
that returns it, e.g.:
class Item : public QGraphicsItem
{
public:
enum { Type = MAGIC_NUMBER };
int type() const { return Type; }
...
};
You can then do this:
QGraphicsItem* item = new Item;
...
Item* derivedItem = qgraphicsitem_cast<Item*>(item);
This will probably help explain what that static_cast is trying to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这看起来像是一种非常可疑的方式来静态断言模板参数
T
具有Type
成员,然后验证其值是预期的幻数,就像您声明的那样应该做的。由于
Type
是一个枚举值,因此不需要this
指针来访问它,因此static_cast- (0)->Type
检索Item::Type
的值,而不实际使用指针的值。所以这有效,但可能是未定义的行为(取决于您对标准的看法,但无论如何,IMO 都是一个坏主意),因为代码使用指针取消引用运算符取消对 NULL 指针的引用(->
) 。但我不明白为什么这比Item::Type
或模板T::Type
更好 - 也许它是设计用于在性能较差的旧编译器上工作的遗留代码模板支持无法弄清楚T::Type
的含义。尽管如此,最终结果是诸如
qgraphicsitem_cast(ptr)
之类的代码将在编译时失败,因为bool
没有Type
成员枚举。这比运行时检查更可靠、更便宜,即使代码看起来像是黑客攻击。This looks like a very dubious way to statically assert that the template parameter
T
has aType
member, and then verify its value is the expected magic number, like you state you are supposed to do.Since
Type
is an enum value, thethis
pointer is not required to access it, sostatic_cast<Item>(0)->Type
retrieves the value ofItem::Type
without actually using the value of the pointer. So this works, but is possibly undefined behavior (depending on your view of the standard, but IMO a bad idea anyway), because the code dereferences a NULL pointer with the pointer dereference operator (->
). But I can't think why this is better over justItem::Type
or the templateT::Type
- perhaps it's legacy code designed to work on old compilers with poor template support that couldn't work out whatT::Type
is supposed to mean.Still, the end result is code such as
qgraphicsitem_cast<bool>(ptr)
will fail at compile time becausebool
has noType
member enum. This is more reliable and cheaper than runtime checks, even if the code looks like a hack.是的,这有点奇怪,并且是官方未定义的行为。
也许他们可以这样写(注意,这里的 T 不再是指针,无论它在原始代码中是否是指针):
但他们可能已经被常量所困扰,并被迫编写同一函数的 2 个版本。也许是他们做出选择的原因。
It's a bit strange, yes, and is officially undefined behavior.
Maybe they could have written it as follows (note that T here is no more a pointer, whether it is a pointer in the original code):
But they may have been bitten by constness, and forced to write 2 versions of the same function. Maybe a reason for the choice they made.
当前标准和即将推出的标准草案表明取消引用空指针是未定义的行为(请参阅第 1.9 节)。由于
a->b
是(*a).b
的快捷方式,因此代码看起来就像尝试取消引用空指针。这里有趣的问题是:static_cast(0)->Type
实际上是否构成了空指针取消引用?如果
Type
是一个数据成员,这肯定会取消引用空指针,从而调用未定义的行为。但根据您的代码Type
只是一个枚举值,而static_cast(0)->
仅用于范围/名称查找。这段代码充其量是有问题的。我发现通过箭头运算符访问“静态类型属性”(例如本地枚举值)很烦人。我可能会以不同的方式解决它:The current standard and the draft for the upcoming standard suggest that dereferencing a null pointer is undefined behaviour (see section 1.9). Since
a->b
is a shortcut for(*a).b
the code looks like it tries to dereference a nullpointer. The interesting question here is: Doesstatic_cast<T>(0)->Type
actually constitute a null pointer dereference?In case
Type
was a data member this would definitely be dereferencing a nullpointer and thus invoke undefined behaviour. But according to your codeType
is just an enum value andstatic_cast<T>(0)->
is only used for scoping / name lookup. At best this code is questionable. I find it irritating that a "static type property" like a local enum value is accessed via the arrow operator. I probably would have solved it differently:这是从(子)类外部使用受保护(静态)成员的常见技巧。更好的方法是公开一些方法,但由于它不是为了成为用户类,所以他们放弃了艰苦的工作?!
This is a common trick to use protected(static) members from outside the (sub)class. A better way would have been to expose some method, but since it wasn't intended to be user class they let go the hard work?!!