C++ is operator!= automatically provided when operator== defined
Asked Answered
M

7

12

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?

Mosira answered 24/7, 2014 at 15:52 Comment(3)
See: https://mcmap.net/q/15363/-what-are-the-basic-rules-and-idioms-for-operator-overloading?rq=1Trolly
This might be possible in a future version of the language: open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4114.htmIsotropic
Related Q : relational operators generator (actually you only need < to have all of them)Mccarley
V
12

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

Vardon answered 24/7, 2014 at 15:55 Comment(1)
But be careful to limit the scope of the using directive. It will provide operators for all types, not just the one you're interested in, which could have surprising consequences.Senega
S
16

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);}
Senega answered 24/7, 2014 at 15:55 Comment(1)
Please see ivan ukr answer.Ebb
V
12

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

Vardon answered 24/7, 2014 at 15:55 Comment(1)
But be careful to limit the scope of the using directive. It will provide operators for all types, not just the one you're interested in, which could have surprising consequences.Senega
B
7

This is true since C++20. Earlier C++ standard versions do not provide operator!= from operator== automatically.

Bourg answered 22/9, 2020 at 19:14 Comment(3)
In C++20 and over, you should actually use the <=> operator then all the other 6 operators are defined in one swoop.Alegre
Except when you don't want operator< and operator>.Karly
I'd say "you can" instead of "you should". That is true, in many cases you'll want to do so, but still not always. Sometimes you just don't need < and >, so you can have only operator==. Moreover, in C++20 you can have operator== defined as = default.Bourg
R
3

What you're after isn't provided by the language for obvious reasons. What you want is provided for by boost::operators:

class MyClass : boost::operators<MyClass> {
    bool operator==(const MyInt& x) const;
}

will get you an operator!=() based on your operator==()

Rowboat answered 24/7, 2014 at 15:57 Comment(0)
R
2

Nope. You have to define it explicitly.

Code:

#include <iostream>

using namespace std;

class a
{
    private:
        int b;
    public:
        a(int B): b(B)
        bool operator == (const a & other) { return this->b == other.b; }
};

int main()
{
    a a1(10);
    a a2(15);
    if (a1 != a2)
    {
        cout << "Not equal" << endl;
    }
}

Output:

[ djhaskin987@des-arch-danhas:~ ]$ g++ a.cpp
a.cpp: In constructor ‘a::a(int)’:
a.cpp:11:9: error: expected ‘{’ before ‘bool’
         bool operator == (const a & other) { return this->b == other.b; }
         ^
a.cpp: In function ‘int main()’:
a.cpp:18:12: error: no match for ‘operator!=’ (operand types are ‘a’ and ‘a’)
     if (a1 != a2)
            ^
a.cpp:18:12: note: candidates are: ...
Roadster answered 24/7, 2014 at 15:57 Comment(1)
Please see ivan ukr answerEbb
H
1

If you #include <utility>, you can specify using namespace std::rel_ops.

Doing this will automatically define operator != from operator ==, and operator <=, operator >=, operator > from operator <.

Hypnosis answered 24/7, 2014 at 15:57 Comment(1)
std::rel_ops is a bit... broken.Isotropic
A
0

No, != is not defined automatically in terms of ==. There are some generics define in that help to define all the operators in term of == and <, though.

Aboriginal answered 24/7, 2014 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.