So I was looking to see if any languages supported overloading a function by return type and found this answer.
The bottom of the post contained this snippet of code that fakes overloading by return type in c++.
struct func {
operator string() { return "1";}
operator int() { return 2; }
};
int main( ) {
int x = func(); // calls int version
string y = func(); // calls string version
double d = func(); // calls int version
cout << func() << endl; // calls int version
func(); // calls neither
} While I would never use this hack in a real project, it does make a fun exercise in understanding c++.
What other examples of strange language hacks do you know of?