jeudi 1 juin 2017

Simplest 'true' value that the compiler doesn't recognise as true

I had a little debug intercept that said

if (true)
    throw new Exception()
DoStuff();

However the compiler warned me that DoStuff() was unreachable code. So were the following:

if (1==1)
if (1 + 1 == 2)

The following 'works'

int x = 1;
if (2 * x == 2)
    throw...

What expression is obviously true to a human yet not to the compiler? The best I could manage was

bool False =>false;
if (False)

...and it has the wonderful property that to switch it off all I have to do is change it to

bool False=>true  /*  :D)     */

Aucun commentaire:

Enregistrer un commentaire