为什么我必须重载operator ==在POD类型? [英] Why do I have to overload operator== in POD types?

查看:132
本文介绍了为什么我必须重载operator ==在POD类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义如下的结构:

I have a struct that's defined like this:

struct Vec3 {
float x, y, z;
}



当我尝试使用 std :: unique std :: vector< Vec3> 上遇到此错误:

描述资源路径位置类型
首先与'operator =='不匹配。 _gnu_cxx :: __ normal_iterator< _Iterator,_Container>与_Iterator = Vec3 *,_Container = std :: vector> == _ next。 _gnu_cxx :: __ normal_iterator< _Iterator,_Container> :: operator * with _Iterator = Vec3 *,_Container = std :: vector >'ModelConverter line 4351,external location:/usr/include/c++/4.4.6/bits/stl_algo.h C / C ++ Problem

Description Resource Path Location Type no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Vec3*, _Container = std::vector > == _next._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Vec3*, _Container = std::vector >’ ModelConverter line 4351, external location: /usr/include/c++/4.4.6/bits/stl_algo.h C/C++ Problem

我理解编译器在中在等于运算符和其他(在这种情况下,*几乎肯定不是我的意思)的naievite的必要性,但这是一个政策问题,或有一个技术原因,它,我不知道?有一个默认的赋值运算符,所以为什么没有默认的相等运算符?

I understand the the necessity of the naievite of the compiler in inequality operators and others (in this case, * would almost certainly not be what I mean), but is this a matter of policy, or is there a technical reason for it that I'm not aware of? There's a default assignment operator, so why no default equality operator?

推荐答案

Pedantically,你可能会说这是因为C不让你比较两个结构与 == ,这是一个很好的理由;当你去C ++的行为切换是非显而易见的。 (可能的,C不支持的原因是,字段比较可能适用于一些结构,但绝对不是全部。)

There's no technical reason. Pedantically, you might say this is because C doesn't let you compare two structures with ==, and this is a good reason; that behavior switching when you go to C++ is non-obvious. (Presumably, the reason that C doesn't support that is that field-wise comparison might work for some structs, but definitely not all.)

从C ++的角度来看,如果你有一个私有字段呢?默认 == 技术上暴露该字段(间接,但仍然)。那么如果没有私有或受保护的数据成员,编译器只生成运算符==

And just from a C++ point of view, what if you have a private field? A default == technically exposes that field (indirectly, but still). So would the compiler only generate an operator== if there are no private or protected data members?

有没有合理的相等定义的类(空类,不模拟状态但缓存它的类等),或者对于谁来说,默认的相等性检查可能会非常混乱(包装指针的类)。

Also, there are classes that have no reasonable definition of equality (empty classes, classes that do not model state but cache it, etc.), or for whom the default equality check might be extremely confusing (classes that wrap pointers).

然后有继承。在继承的情况下决定如何做 operator == 很复杂,编译器很容易做出错误的决定。 (例如,如果这是C ++做的,我们可能会得到关于为什么 == 总是成功的问题,当你测试两个对象之间的平等是抽象的后代基本类,并使用它的引用。)

And then there's inheritance. Deciding what to do for operator== in a situation of inheritance is complicated, and it'd be easy for the compiler to make the wrong decision. (For example, if this was what C++ did, we would probably be getting questions about why == always succeed when you test equality between two objects that are both descendants of an abstract base class and being used with a reference to it.)

基本上,这是一个棘手的问题,它是更安全的编译器保持离开,即使考虑到你可以覆盖编译器决定的内容。

Basically, it's a thorny problem, and it's safer for the compiler to stay out of it, even considering that you could override whatever the compiler decided.

这篇关于为什么我必须重载operator ==在POD类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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