C.86: Make == symmetric with respect to operand types and noexcept
C.86:保证==语义遵守操作数规则并不会抛出异常
Reason(原因)
Asymmetric treatment of operands is surprising and a source of errors where conversions are possible.== is a fundamental operations and programmers should be able to use it without fear of failure.
操作数的非对称处理会令人诧异而且成为错误的源头当可能发生类型转换时。==是一个基础的操作而且程序员应该可以使用它而不必担心失败。
Example(示例)
Example, bad(反面示例)
B’s comparison accepts conversions for its second operand, but not its first.
B的比较运算符可以接受第二个操作数的类型转换,但无法接受第一个参数的类型转换。
Note(注意)
If a class has a failure state, like double’s NaN, there is a temptation to make a comparison against the failure state throw. The alternative is to make two failure states compare equal and any valid state compare false against the failure state.
如果一个类有失败状态,就像双精度数的NaN,就会产生一种诱惑在和失败状态对象比较是抛出异常。另外一种选择是将两个失败状态的比较结果视为相等,有效状态和无效状态的比较结果视为不相等。(而不抛出异常,译者注)
Note(注意)
This rule applies to all the usual comparison operators: !=,
这条规则同样被适用于通常的比较运算符:!=,
Enforcement(实施建议)
- Flag an operator==() for which the argument types differ; same for other comparison operators: !=,
- Flag member operator==()s; same for other comparison operators: !=,
原文链接
你觉得这篇文章有帮助吗?欢迎与更多的人点赞和分享。
[En]
Think this article is helpful? Welcome to like and share with more people.
更多更新,请关注微信公众号[面向对象思维]
[En]
For more updates, please follow the official account of Wechat [object-oriented thinking]
Original: https://blog.51cto.com/oothinking/5527994
Author: 大连木匠
Title: C++核心准则C.86:保证==语义遵守操作数规则并不会抛出异常
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508433/
转载文章受原作者版权保护。转载请注明原作者出处!