Friday, August 5, 2011

Thursday, August 4, 2011

C/C++, #Ifdef, working around the need for them

Consider the all to common code:



#ifdef HAVE_SOMETHING


something();


#endif



ifdef are problematic because any number of combinations of them can lead to multiple execution paths that the compiler never will so. PIA


The above assumes that HAVE_SOMETHING will have some value or not. 


Another option is the following:



if (HAVE_SOMETHING)


{


  something();


}


The significance in the above?


Any good compiler will evaluate HAVE_SOMETHING, and if the value is zero then the code path will be optimized out.


Awesome.