[英]Why can't this std::find compare these objects
Usually when I do an std::find I'll put a predicate as the third argument, but this time I thought I'd do it differently, I don't understand why it doesn't work. 通常当我做一个std :: find时我会把一个谓词作为第三个参数,但这次我以为我会用不同的方式做,我不明白为什么它不起作用。
#include <iostream>
#include <vector>
#include <algorithm>
struct RenderJob
{
RenderJob() {};
int renderJob_ID;
bool operator==(RenderJob& rhs) { return rhs.renderJob_ID == this->renderJob_ID; }
};
int main()
{
RenderJob foo;
RenderJob foo2;
foo == foo2; // Works
std::vector<RenderJob> renderJobs;
std::find(renderJobs.begin(), renderJobs.end(), foo); // Doesn't work
}
binary "==" no operator found which takes a left-handed operand of type RenderJob (or there is no acceptable conversion) 二进制“==”没有找到哪个运算符采用RenderJob类型的左手操作数(或者没有可接受的转换)
Edit:: Well thanks for the answers. 编辑::谢谢你的答案。 Here are some examples of why it fails 以下是一些失败原因的例子
RenderJob foo;
RenderJob foo2;
foo == foo2; // Works
std::vector<RenderJob> renderJobs;
std::vector<RenderJob>::const_iterator constit = renderJobs.begin();
*constit == foo2; // Doesn't work
Even simpler as an illustration: 更简单的说明:
const RenderJob* pToRenderJob;
*pToRenderJob == foo2; // This fails because the pointed to
// object is const, and cannot call the
// operator== function because the actual function
// definition is not const.
If it were the other way around: 如果是相反的方式:
foo2 == *pToRenderJob; // This would fail because the
// operator==(RenderJob&) the actual argument
// is not const. Very subtle rules
你离开了const
限定符。
bool operator==(const RenderJob& rhs) const { ... }
Looks to me like a const-correctness problem. 在我看来像一个常量问题。 Try something like this instead: 尝试这样的事情:
bool operator==(RenderJob const &rhs) const {
return rhs.renderJob_ID == this->renderJob_ID;
}
Doing the comparison yourself worked because you were just passing plain objects that weren't temporaries, nor were they const qualified. 做比较你自己工作,因为你只是传递不是临时的普通对象,也不是const限定。 With std::find
, the comparison function will (at least usually) receive a reference to the object in the collection (which will normally be const qualified), so it needs to be able to receive a const-qualified reference. 使用std::find
,比较函数将(至少通常)接收对集合中对象的引用(通常是const限定的),因此它需要能够接收const限定引用。
But that still doesn't make sense, in the above example, the right hand side is
foo2
, which is not constant, which should go to the non-const
operator==
. 但是这仍然没有意义,在上面的例子中,右边是foo2
,它不是常量,应该转到非const
operator==
。 Unless there's a general rule thatconst
and nonconst
can't be compared. 除非有一般规则不能比较const
和非const
。
There is no rule in the language that const
and non- const
objects cannot be compared. 语言中没有规则不能比较const
和非const
对象。 You have to make sure that they can be compared by using proper const
qualifiers. 您必须确保可以使用适当的const
限定符来比较它们。
The line 这条线
*pToRenderJob == foo2;
is equivalent to 相当于
pToRenderJob->operator==(foo2);
That does not work since pToRenderJob
cannot be used to call a non- const
member function. 这不起作用,因为pToRenderJob
不能用于调用非const
成员函数。 foo2
is not the problem here. foo2
不是问题所在。
If you use 如果你使用
foo2 == *pToRenderJob
it is equivalent to 它相当于
foo2.operator==(*pToRenderJob)
That is also a problem since the argument to the function is a const
object while your function expects a non- const
reference. 这也是一个问题,因为函数的参数是一个const
对象,而你的函数需要一个非const
引用。 Once again, foo2
is not the problem. 再一次, foo2
不是问题。
Making the function a const
member function and making the argument a const
reference makes sure that all combinations of const
and non- const
objects on both sides of the operator can be used without any problem. 使函数成为const
成员函数并使参数成为const
引用可确保操作符两侧的const
和非const
对象的所有组合都可以毫无问题地使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.