I wonder if operator!= is automatically provided when operator== is defined within my class? When I have operator== defined in class A, obviously A a, A b, a == b works, but a != b doesn't. However I am not sure if it always happens. Are there any exceptions from this?
No, operators (apart from assignment) are never automatically generated. It's easy enough to define it in terms of ==
:
bool operator!=(A const & l, A const & r) {return !(l == r);}
The operator !=
is not automatically provided for you. You may want to read about rel_ops namespace if you want such automation. Essentially you can say
using namespace std::rel_ops;
before using operator !=
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With