7

I have a piece of code that implemented the == overload. And I had to use it somewhere negated. But writing !(A == B) does not seem really clear for the reader. So I implemented this double overloading instead. Does it have any drawbacks of doing it this way? Efficiency wise?:

struct Foo{
  bool operator==(const Foo& other) const{
      .... //Something that sometimes produces "return false"
      return true;
  }
  bool operator!=(const Foo& other) const{
      return !(*this == other);
  }
}

Bonus: Why does the compiler not already default implement != using the negation of == ?

DarkZeros
  • 8,235
  • 1
  • 26
  • 36
  • 1
    Re Bonus: Because C/C++ does not do things that you don't tell it to do (yes this is a gross oversimplification -- more a description of the C/C++ philosophy than the actual fact.) If it generated operator != based on your implementation of operator == it might well get it wrong! – Dale Wilson Jun 27 '17 at 16:08
  • 5
    @DaleWilson C/C++ is *not* a thing! It's either C, or C++, but not both. Also, C doesn't even have operator overloading, so it doesn't even make sense to mention it. Just use C++ next time please. – Rakete1111 Jun 27 '17 at 16:10
  • For your bonus question see: https://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator – NathanOliver Jun 27 '17 at 16:16
  • 1
    its an extra function call that may be inlined or not, but otherwise exactly the same. I would look at the output of the compiler (eg https://godbolt.org/) any thing else is just speculations – 463035818_is_not_an_ai Jun 27 '17 at 16:17
  • 1
    @RSahu Are you sure you want to dupe close to a C# Q&A? – NathanOliver Jun 27 '17 at 16:17
  • @NathanOliver, the rationale is exactly same. I didn't want to copy and paste the same code and same language from that other post. – R Sahu Jun 27 '17 at 16:18
  • 6
    The core reason is that **When you override != and ==, you do not have to return bool** – R Sahu Jun 27 '17 at 16:19
  • This used to be a duplicate of https://stackoverflow.com/questions/4421706/operator-overloading instead of https://stackoverflow.com/questions/6916884/why-must-we-define-both-and-in-c, and the first is probably still better for the main question instead of the bonus. – Daniel H Jun 27 '17 at 16:22
  • @tobi303 I can’t imagine a compiler not inlining that on any level except `-O0`, and maybe even then. – Daniel H Jun 27 '17 at 16:24
  • 2
    @DanielH me neither, but sometimes I prefer to check rather than relying on my imagination ;) – 463035818_is_not_an_ai Jun 27 '17 at 16:27
  • @tobi303 And it’s a good thing you do, because [apparently GCC only enables that at `-O2` and above](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fno-inline) unless the operator is only called once or it decides the function body is smaller than the function call overhead (which I doubt, because it has to have that overhead to call `operator==`). – Daniel H Jun 27 '17 at 16:38
  • 1
    @Rakete111 C/C++ is a tradition, not a thing. C++ attempts to preserve the spirit of C -- which was a reaction to the "everything including the kitchen sink" compilers that came before it. – Dale Wilson Jun 27 '17 at 17:35
  • @RSahu You don't have to copy/paste. If you want to define operator != in terms of operator == (or vice versa) using templates you are welcome to do so. Just don't expect it from the language. – Dale Wilson Jun 27 '17 at 17:37
  • @DaleWilson, I didn't mean copy and paste code in the implementations of the functions. I meant copy and paste code from the other post. – R Sahu Jun 27 '17 at 17:38
  • @DaleWilson "C++ attempts to preserve the spirit of C" Last time I checked, no it doesn't. C++ has a *way* different philosophy than C. Just because C++ has almost all features from C, it doesn't mean that it tries to follow its example or something. – Rakete1111 Jun 27 '17 at 17:55
  • @Rakete1111 There are several philosophical differences, but there’s also a lot of philosophical similarity. You won’t find either of them adding a garbage collector by default, like you do in other languages; C++ only recently got some constructs which could support it, and few if any people use those constructs. – Daniel H Jun 27 '17 at 20:17

0 Answers0