On 17 Feb 2005 13:09:19 -0800, Generic Usenet Account
<us****@sta.samsung.com> wrote:
I have written a small macro that provides the relative offset of any
field within a structure. Here it goes:
Well, quite apart from the fact that the offsetof() feature does it more
portably, no one says that macros are totally useless. You illustrate
one of the uses, to make a string from a name which can't be done any
other way than by #x in a macro.
To all those who believe that inlining is a much better option I say
---- macros ain't that bad.
Macros are evil, not bad. Sometimes they are the lesser of two or more
evils. The problem with them is that they are too frequently used for
situations where inline functions (and template functions) are a better
solution, often because the programmer is really still writing C instead
of C++. For instance:
template <class T>
inline T abs(const T& a)
{
return (a < 0 ? -a : a);
}
versus:
#define abs(a) (a < 0 ? -a : a)
The latter evaluates its argument twice, which causes problems if the
argument has side-effects (abs(++x) for instance), the former is
type-safe and only evaluates its argument once. It can even be
overridden with a special implementation, for instance for abs of a
string (remove a leading minus sign, or something) which isn't possible
with a macro.
Chris C