I understand C++ can’t define operator== automatically for a class, but why can’t it use !(a == b) for a != b when operator!= isn’t available but operator== is?
I’m aware of std::rel_ops although I hadn’t heard of it before today.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because
operator==does not necessarily mean the opposite ofoperator!=.I cannot think of any instance where
operator==would not mean!operator!=, but they are separate operators. One of the most liberating and, at times, most frustrating things about C++ is that C++ applies a minimal set of restrictions about how you can write your code. If you have an instance whereoperator==is not the opposite ofoperator!=, then you should be able to express that in C++. And, in fact, you can.You take the good with the bad in C++. You may consider this to be in the set of “the bad”.
Bear in mind that in the vast majority of cases, it is trivial to correctly implement
operator!=in terms ofoperator==.