简体   繁体   English

比较地址足够这个`operator ==`?

[英]Is comparing addresses enough for this `operator==`?

I am creating a custom RTTI system for my event system. 我正在为我的活动系统创建一个自定义RTTI系统。 Below is the EventTypeInfo class. 下面是EventTypeInfo类。 As you can see, it is noncopyable, just as std::type_info . 如您所见,它是不可复制的,就像std::type_info

class EventTypeInfo
{
  public:
    EventTypeInfo(const EventTypeInfo&) = delete;
    EventTypeInfo& operator=(const EventTypeInfo&) = delete;

    inline bool operator==(const EventTypeInfo& other) const {
        return this == &other;
    }
};

The way I create these objects for every event class boils down to this: 我为每个事件类创建这些对象的方式归结为:

template<class EventClass>
const EventTypeInfo& event::type::info()
{
    static EventTypeInfo typeinfo;
    return typeinfo;
}

Given that (1) these objects are created statically (which means they will last for the entire duration of the application), (2) they are noncopyable, and (3) there's no way to modify an EventTypeInfo 's fields without resorting to const_cast , is it enough for me to implement operator== is terms of this == &other , or did I miss something? 鉴于(1)这些对象是静态创建的(这意味着它们将持续整个应用程序的持续时间),(2)它们是不可复制的,并且(3)没有办法修改EventTypeInfo的字段而不诉诸const_cast ,是否足以让我实现operator== this == &other条款,还是我错过了什么?

If you are sure that there will ever only be one instance of each type, the address compare will be ok. 如果您确定每种类型只有一个实例,那么地址比较就可以了。

It is not enough for std::type_info as its uniqueness is not guaranteed by the standard. 对于std :: type_info来说,这还不够,因为标准不保证其唯一性。

In case of a multi-modular application, I think each module might end up with its own copies of static type-info objects. 在多模块化应用程序的情况下,我认为每个模块可能最终都有自己的静态类型信息对象副本。 Therefore just comparing an address might be not enough if your events can fly between modules. 因此,如果您的事件可以在模块之间飞行,那么仅仅比较地址可能是不够的。

As far as I know, RTTI system in GCC had this sort of problem in past , and it caused troubles. 据我所知, 海湾合作委员会的RTTI系统过去曾遇到过这类问题,造成了麻烦。 Eventually, GCC developers admitted it and fixed to use string comparison in GCC 4.5. 最终, GCC开发人员承认并修复了在GCC 4.5中使用字符串比较

So I would suggest you to do pointer comparison first, and if it fails, then check with a more reliable mechanism. 所以我建议你先做指针比较,如果失败了,那就检查一下更可靠的机制。

Semantics of Equal (deep or shallow) 等于(深或浅)的语义

In the general case, I think it's not quite enough. 在一般情况下,我认为这还不够。

Consider the case where you have two different instances (addresses), but the same guts/internals. 考虑你有两个不同的实例(地址),但相同的内部/内部的情况。

  • Do you want this to return true (what people normally expect, let's say for two strings both with the value "foo" but instantiated at different times)? 你想让它返回true(人们通常期望的是,让我们说两个字符串都是值“foo”但是在不同的时间实例化)?
  • Or false (what your short-cut would return)? 或者是假(你的捷径会回归)?

If it's really the latter, then maybe your short-cut would work: your short-cut is really one type of a shallow comparison . 如果它真的是后者,那么也许你的捷径可行:你的捷径实际上是一种浅薄的比较

For instance, if you can guarantee that every event would have a different timestamp anyways, which would cause two different instances to return false anyhow (and you care about timestamp for comparison purposes), using the short-cut would be fine. 例如,如果您可以保证每个事件都有不同的时间戳,这将导致两个不同的实例无论如何都返回false(并且您关心时间戳以进行比较),使用快捷方式会很好。

Otherwise, do it the normal way ( a deep comparison or at least a one-level shallow comparison ). 否则,按照正常方式进行( 深度比较或至少一级浅比较 )。

Ultimately, it comes down to your interpretation/semantics of what equals means, in this case. 归根结底,在这种情况下,它取决于你对等于什么意思的解释/语义。

If you do choose to compare addresses, I recommend that you put comments about your implementation so that future readers of your code will understand that you have put thought into your implementation and this is really what you intended. 如果选择比较地址,我建议你把评论你的实施,使你的代码的未来读者会明白,你已经把想法变成你的实现,这是你真的打算什么。

You haven't shown us enough of the class to be sure. 你还没有向我们展示足够的课程。 Without any data members, there's nothing but the address to determine if they're equal or not. 没有任何数据成员,没有什么,但地址以确定它们是否相等。 If there are data members that you haven't included in your example, is it possible to create two instances that contain the same data? 如果您的示例中没有包含数据成员,是否可以创建包含相同数据的两个实例? Should these instances compare as equal? 这些实例应该相等吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM