C ++ core principles C.86: == Semantic ensure compliance with the rules of the operand does not throw an exception

C.86: Make == symmetric with respect to operand types and noexcept

C.86: == Semantic ensure compliance with the rules of the operand does not throw an exception

 

 

Reason (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.

 

 

 

 

Asymmetric treatment will be surprising operand and the source of error can occur when the type of conversion to be. == it is a basic operation and programmers should be able to use it without fear of failure.

 

 

 

Example (Example)

 

struct X {
    string name;
    int number;
};

bool operator==(const X& a, const X& b) noexcept {
    return a.name == b.name && a.number == b.number;
}

 

 

Example, bad (negative sample)

 

class B {
    string name;
    int number;
    bool operator==(const B& a) const {
        return name == a.name && number == a.number;
    }
    // ...
};

B's comparison accepts conversions for its second operand, but not its first.

B comparison operators acceptable type conversions second operand, but we can not accept the first type of transformation parameters.

 

 

 

Note (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.

If a class has a failed state like NaN double-precision numbers, it will produce a temptation and a failed state in the object comparison is thrown. Another option is to compare the results of two failed states are considered equal, the comparison result of active and inactive states not considered equal. (Without throwing an exception, Translator's Note)

 

 

 

Note (Note)

 

This rule applies to all the usual comparison operators: !=, <, <=, >, and >=.

The same rule is applicable to the usual comparison operators:! =, <, <=,>, And> =.

 

 

Enforcement (Suggestions)

 

  • Flag an operator==() for which the argument types differ; same for other comparison operators: !=, <, <=, >, and >=.

    If the parameter is equal to other types of operators, prompt. Other comparison operators, too:! =, <, <=,>, And> =.

  • Flag member operator==()s; same for other comparison operators: !=, <, <=, >, and >=.

    Marking member function comparison operators, other comparison operators, too:! =, <, <=,>, And> =.

 

 

Description link

 

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c86-make--symmetric-with-respect-to-operand-types-and-noexcept

 


 

I think this article helpful? Welcome thumbs up and share it with more people.

Read more updated articles, please pay attention to micro-channel public number of object-oriented thinking []

Published 408 original articles · won praise 653 · views 290 000 +

Guess you like

Origin blog.csdn.net/craftsman1970/article/details/104905003