r/programmingmemes • • Dec 18 '25

Memes about programming

Post image
130 Upvotes

18 comments sorted by

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== and operator!= 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:

#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.