Overloading logical operators considered bad practice?

前端 未结 7 671
星月不相逢
星月不相逢 2021-01-11 13:40

Is it a bad idea to overload &&, || or comma operator and Why?

相关标签:
7条回答
  • 2021-01-11 14:39

    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.

    0 讨论(0)
提交回复
热议问题