Missed Opportunity for Easter Egg
-
In VB that expression will always resolve to False In other languages (C included) you could overload the values for 1 and 2 and/or the operator = or command If Good luck figuring out whats going on with that level of obfuscation. Its not something I would expect to see as a simple expression but it can be useful as a clause especially if you need to build up a complex expression at run time. In practice its more likely that some piece of funcionality is wanted to remain in the codebase but not required to run under normal circumstances - probably debug code although there are better ways of doing it.
greldak wrote:
In other languages (C included) you could overload the values for 1 and 2 and/or the operator = or command If
Good luck figuring out whats going on with that level of obfuscation.I'm pretty sure you're wrong about being able to override either the values or the operator in C (or even C++ for that matter), but I'd welcome a working example.
-
or 9 x 6 = 42
-
I just came across this code written by a coworker:
If 1 = 2 Then
Seems silly; probably a way to temporarily block out some code or something of that sort. However, it made me wonder why there aren't more people taking advantage of a potential easter egg. When I typed in "2+2" into Google and WolframAlpha, they both replied with "4". :((
-
Oh yes! I remember making that mistake - it took ages to find before I realized that my constant values had changed... Trouble was,
1=2
looked a lot like
I=2
:doh:
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
OriginalGriff wrote:
... my constant values had changed...
Something about that just doesn't seem right ...
-
AspDotNetDev wrote:
If 1 = 2 Then
In old-school Fortran this was a real potential. Certain integers were stored at memory locations, then you could say 1 = 2 (assign the value 2 to where the value 1 is stored). Then statements like
if 1 = 2 then ...
would be 'valid'.
If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering” - Wernher von Braun -
OriginalGriff wrote:
... my constant values had changed...
Something about that just doesn't seem right ...
That's why it took so long to work it out - it's just not something you expect... :laugh:
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
Neat trick. :thumbsup:
-
greldak wrote:
In other languages (C included) you could overload the values for 1 and 2 and/or the operator = or command If
Good luck figuring out whats going on with that level of obfuscation.I'm pretty sure you're wrong about being able to override either the values or the operator in C (or even C++ for that matter), but I'd welcome a working example.
compiles on GCC
#define if(X) if(1)
using namespace std;
int main()
{
if (1 == 2) {
cout << "this is executed" << endl;
}
return 0;
}this would compile on C compiler too, but I just hate using those printfs. But you were right about #defining numbers -- it's impossible. A define identifier name cannot start with a number. But you can overload almost anything else.
-
compiles on GCC
#define if(X) if(1)
using namespace std;
int main()
{
if (1 == 2) {
cout << "this is executed" << endl;
}
return 0;
}this would compile on C compiler too, but I just hate using those printfs. But you were right about #defining numbers -- it's impossible. A define identifier name cannot start with a number. But you can overload almost anything else.
-
Hmmm, yeah, well what you've really done is macro replaced an "if" expression (not really overloaded it in the normal sense of the word, but I guess close enough), but have not really succeeded in overloading 1, 2, or ==.
Even through it does not really "overload" it, it can still lead to a lot of confusion. In C++ you can also do something like this
#include
class T {
public:
int a;
T(int _a) : a(_a) {}
bool operator==(const T &var) {return true;}
};using namespace std;
int main() {
#define int T //this is defined here because main returns int, and this causes error if defined earlier
int a = 1, b = 2;
if (a == b) {
cout << "this is evaluated" << endl;
}
return 0;
}
}Of course, you can always put anything in the == operator so it would behave differently :)
-
Even through it does not really "overload" it, it can still lead to a lot of confusion. In C++ you can also do something like this
#include
class T {
public:
int a;
T(int _a) : a(_a) {}
bool operator==(const T &var) {return true;}
};using namespace std;
int main() {
#define int T //this is defined here because main returns int, and this causes error if defined earlier
int a = 1, b = 2;
if (a == b) {
cout << "this is evaluated" << endl;
}
return 0;
}
}Of course, you can always put anything in the == operator so it would behave differently :)
If you want to only "subtly" mess it up, and work for most of other cases normally you can do this:
#include
class T {
public:
int a;
T(int _a) : a(_a) {}
bool operator==(const T &var) {
if((a == 1 && var.a == 2) || (a == 2 && var.a == 1)) return true;
else return a == var.a;
}
};using namespace std;
int main() {
#define int T //this is defined here because main returns int, and this causes error if defined earlier
int a = 1, b = 2;
if (a == b) cout << "this is evaluated";
a = 4, b = 5;
if (a == b) cout << "while this is not" << endl;
return 0;
} -
:laugh: :thumbsup:
-
greldak wrote:
In other languages (C included) you could overload the values for 1 and 2 and/or the operator = or command If
Good luck figuring out whats going on with that level of obfuscation.I'm pretty sure you're wrong about being able to override either the values or the operator in C (or even C++ for that matter), but I'd welcome a working example.
In C++ you can override operators inside of a class.
class foo
{
private:
int num;public:
foo* operator=(int i) {
this->num = i;
return this;
}
// ...
};I believe the example is syntactically correct and it is a very contrived an arbitrary example. Numbers cannot be overloaded (In Scheme maybe?) and new operators cannot be created that do not already exist. In C operators cannot be overloaded.
-
If you want to only "subtly" mess it up, and work for most of other cases normally you can do this:
#include
class T {
public:
int a;
T(int _a) : a(_a) {}
bool operator==(const T &var) {
if((a == 1 && var.a == 2) || (a == 2 && var.a == 1)) return true;
else return a == var.a;
}
};using namespace std;
int main() {
#define int T //this is defined here because main returns int, and this causes error if defined earlier
int a = 1, b = 2;
if (a == b) cout << "this is evaluated";
a = 4, b = 5;
if (a == b) cout << "while this is not" << endl;
return 0;
}Yep, I'm aware of operator overloading and all the attendant mischief possible with it. But I stand by my original statement, that given the original expression 1 == 2, there is no way in C or C++ to overload the values of the integer literals nor is there a way to redefine the meaning of == in a way that would affect it's meaning when used on those literals. But as you cleverly pointed out, you can use the macro processor to redefine if in such a way as to eliminate the expression before the compiler ever sees it.