Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ is operator!= automatically provided when operator== defined

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?

like image 790
thatsme Avatar asked Jul 24 '14 15:07

thatsme


2 Answers

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);}
like image 113
Mike Seymour Avatar answered Sep 21 '22 17:09

Mike Seymour


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 !=.

like image 22
Wojtek Surowka Avatar answered Sep 20 '22 17:09

Wojtek Surowka