重载逻辑运算符被认为是不好的做法吗?
重载 &&, || 是不是一个坏主意?或逗号运算符和为什么?
Is it a bad idea to overload &&, || or comma operator and Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
重载 &&, || 是不是一个坏主意?或逗号运算符和为什么?
Is it a bad idea to overload &&, || or comma operator and Why?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
我不会重载
operator&&
或operator||
。即使您定义了一个产生布尔代数(例如有限集)的类,重载operator&
和operator|
也可能是更好的选择。原因是 C++ 程序员期望
operator&&
和operator||
具有特殊的语义:它们是短路的,即不如果没有必要,评估他们的右手论证。您无法通过重载来获得此行为,因为您将定义一个函数。重载
运算符,
已在例如Boost.Assign 库。这也是我所知道的唯一一个重载的例子,而且我自己从来没有考虑过重载它。您最好有一个非常具体的用例,没有其他运算符适合。I wouldn't overload
operator&&
oroperator||
. Even if you define a class that gives rise to a Boolean algebra (finite sets, for example), it would probably be a better choice to overloadoperator&
andoperator|
.The reason is that C++ programmers expect special semantics for
operator&&
andoperator||
: they are short-circuited, i.e. don't evaluate their right-hand argument if not necessary. You can't get this behavior by overloading, since you'll be defining a function.Overloading
operator,
has been done in e.g. the Boost.Assign library. This is also the only example of its overloading that I know, and I've never even considered overloading it myself. You'd better have a very specific use case for it where no other operator is suited.为了重载 C++ 中的逻辑运算符,必须对操作数进行求值,这不是内置类型短路通常的工作方式。
请看下面的链接。
For overloading the logical operators in C++, the operands must be evaluated, which isn't the way things normally work with short circuiting of built-in types.
Look at the below link.
您不应该以令人惊讶的方式重载任何运算符。 :-)
如果你能以一种有意义的方式来做这件事(不仅对你来说),那么这样做就很好。
正如其他人所说,逻辑运算符的特殊之处在于它们具有惰性求值的效果。因此,您的重载可能应该保留这种惰性效果,就像使用表达式模板一样,或者仅在人们不期望这种效果的情况下使用。
You shouldn't overload any operators in a way that is surprising. :-)
If you can do it in a way that makes sense (not only to you), it is fine to do so.
Like others have said, the logical operators are special in that they have the effect of lazy evaluation. So your overloads should probably preserve this lazy effect, like with expression templates, or only be used where people don't expect this effect anyway.
这通常是一个坏主意:这三个运算符具有排序效果,当您过载它们时,这种效果就会消失。失去排序效果可能会导致那些没有预料到会失去这种效果的人(即奇怪的错误)。
在某些情况下,模板表达式可以保持排序效果,在这些情况下,我认为重载它们没有问题。
据我所知,运算符的重载还有另一个问题:它们的工作方式使得表面上的操作链并不是真正的操作链。通常,它们在没有区别的情况下使用,但一旦出现,那就是奇怪错误的另一个来源。
It is usually a bad idea: those three operators have a sequencing effect which is lost when you overload them. Loosing that sequencing effect can cause arm (i.e. strange bugs) to those not expecting that lost.
There are cases with template expressions where you can keep the sequencing effect, in those cases I see no problem in overloading them.
The overloads of
operator,
I know of have another problem: they work in such a way that the apparent chains of operation isn't the real one. Usually, they are used in context when it makes no difference, but once in a blue moon, that is another source of a strange bugs.我想说这取决于你的超载在做什么。例如,&&和 || 被期望作为逻辑条件工作,因此,如果您的重载语义以某种方式不同,它们可能会混淆其他人(甚至您自己,如果您暂时不使用它们并忘记它们的作用)。考虑一下,如果您不知道运算符如何重载,以及使用普通方法是否会更清楚,您希望运算符执行什么操作。
I'd say it depends on what your overloads are doing. For example, && and || are expected to work as logical conditions, so if your overload semantics work somehow differently, they can confuse other people (or even yourself, if you don't use them for a while and forget what they do). Consider what you would expect the operators to do if you wouldn't know how they are overloaded and if it would be clearer to just use normal methods instead.
正如其他人所说,缺少惰性求值是避免重载逻辑运算符的主要原因。
然而,有一个很好的理由来重载它们:表达式模板。 Boost.Lambda 库可以做到这一点,而且非常有用!
As the others have said, missing lazy evaluation is the main reason to avoid overloading logical operators.
However, there is one very good reason to overload them: Expression templates. The Boost.Lambda library does this, and it's very useful!
这是个坏主意,除非你的类代表某些逻辑实体,因为重载的运算符会迷失方向,并可能导致代码中出现新的错误。
Its bad idea except situations when your classes represents some logic entity, because overloaded operators will disorientate and can cause new bugs in code.