I have a library that I am trying to clean up (it's got way too much preprocessor in it.) I wanted to ask here to be sure that the replacements I'm making will not show any performance problems.
Preprocessor
ConstsCode:#define AMOUNT 5 #define INC int foo(int i){ #ifdef INC return i + AMOUNT; #else return i - AMOUNT; #endif }
TemplatesCode:const int AMOUNT = 5; const bool INC = true; int foo(int i){ if (INC){ return i + AMOUNT; } return i - AMOUNT; //not in an else to prevent compiler warnings about reaching end of non-void method }
Each of these approaches should end up producing the same instructions correct? All of the decisions end up being made at compile time and the compiler will remove the compiled code that is inaccessible. I will end up using a mix of const and templates depending on the circumstances of the change, but since it's a time critical library, I want to make sure there is no performance degradation.Code:template <bool INC, int AMOUNT> int foo(int i){ if (INC){ return i + AMOUNT; } return i - AMOUNT; //not in an else to prevent compiler warnings about reaching end of non-void method }




Reply With Quote