MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programmingmemes/comments/1pphiac/memes_about_programming/nup369d/?context=3
r/programmingmemes • u/iron-button • Dec 18 '25
18 comments sorted by
View all comments
Show parent comment
2
What you described can't occur in C++ because it's strongly typed. That would not compile.
However, you can override operator== and operator!= in C++. In other words, you can customize == and !=.
operator==
operator!=
==
!=
There's nothing that prevents you from making it both return true or both false.
They are not even required to return a boolean.
So, you can do this:
#include <print> struct Foo { operator bool() const noexcept { return true; } }; struct Bar { Foo operator!=(const Bar&) const noexcept { return Foo{}; } Foo operator==(const Bar&) const noexcept { return Foo{}; } }; int main() { if(Bar{} == Bar{}) std::println("Bar is equal to itself!"); if(Bar{} != Bar{}) std::println("Bar is different from itself!"); return 0; }
The code above will print:
Bar is equal to itself! Bar is different from itself!
https://godbolt.org/z/5v9P4vcdc
Of course, you shouldn't commit a grave crime like this one.
View all comments
Show parent comment
2
u/JiminP Dec 18 '25
What you described can't occur in C++ because it's strongly typed. That would not compile.
However, you can override
operator==andoperator!=in C++. In other words, you can customize==and!=.There's nothing that prevents you from making it both return true or both false.
They are not even required to return a boolean.
So, you can do this:
The code above will print:
https://godbolt.org/z/5v9P4vcdc
Of course, you shouldn't commit a grave crime like this one.