2008 Mar 16 - Sun
Don't Use Defined Macros, Use Templated Inline Functions Instead
In the olden C days, one would use #define MACRO .... to build an inline macro
for computationally quick evaluation of some calculation. When using that method
of programming, one needed to remember to parenthesize extensively in order to prevent
wierd things from happening when calling the function with an expression.
The modern approach is to use a template for an inline function, which yields
all the efficiency of a macro, plus all the predictable behavior and type safety
of a regular function (item #2 in the book Effective C++. An example declaration follows:
template<typename T>
inline void DoWithMax( const T& a, const T& b ) {
f( a > b ? a : b );
}
|