TITLE: how to make if (false) survive the optimizer (Source: comp.lang.c++.moderated, 19 Jan 2001) FORMAN: Pete Forman > Code such as this may be optimized out by the compiler. > > if (false) { > do_something(); > } > > So my question is, what expression can be used in place of "false" in > the above? "1 == 0" is easily spotted by the compiler. CLAMAGE: "Steve Clamage" Compilers often have options to leave dead code in place. In addition, when compiling for debug, many compilers won't optimize away such code. If want the dead code in place when not debugging, you'll have to experiment with non-constant expressions to find at what point you can defeat the optimizer. bool f = false; if( f ) ... An optimizer that does data-flow analysis won't be fooled by that one. You could then try static bool f = false; void foo() { if( f ) ... } An optimizer that does global data-flow analysis might not be fooled by that attempt either. You could then try static bool f() { return false; } void foo() { if( f() ) ... } Some optimizers will figure out this ruse as well. You could then try making f a global function in a different module, and have it seem to depend on global variables. int i=1, j=1; bool f() { return i-j; } Creators of performance test suites (like Spec) have to resort to such complications to ensure that optimizers don't simply throw out all the code that in fact does nothing useful. _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com