Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any usages of default operator!=, operator< ...?

Let simplify to operator== and operator!=.

If I have ..operator==..=default; then I do not need to have the operator!= to be defaulted.

struct S {
  bool operator==(const S&) const = default;
  // the line below not needed to do S{} != S{}
  bool operator!=(const S&) const = default;
};

If I do not have ..operator==..=default; then adding ..operator!=..=default; is pointless because it is deleted anyway - and I can't do !=

struct S {
  // allowed - but it is deleted when neither operator<=> defaulted nor operator== defined
  bool operator!=(const S&) const = default;
};

S{} != S{}; // does not compile - operator!= is deleted

Same is for operator<=> and all operator<, operator<=, ...

So the question. Is there any real usage of writing any of these:

  bool operator!=(const S&) const = default;
  bool operator<(const S&) const = default;
  bool operator>(const S&) const = default;
  bool operator<=(const S&) const = default;
  bool operator>=(const S&) const = default;
like image 822
PiotrNycz Avatar asked Dec 21 '25 22:12

PiotrNycz


1 Answers

If you have some fixed ABI:

// header
struct X {
    // ...
    friend bool operator==(const X&, const X&);
    friend bool operator!=(const X&, const X&);
};
// source
bool operator==(const X& l, const X& r) { /* ... */ } 
bool operator!=(const X&, const X&) = default;

Or if you wish to take the address of operator!= (say to pass to some algorithm), which wouldn't exist if you didn't define it. This is the reason given in P0515R3:

Note =default for two-way operators is useful to conveniently force the creation of functions whose addresses can be taken.

like image 71
Artyer Avatar answered Dec 24 '25 11:12

Artyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!