On Jul 27, 11:05*am, Nobody <nob...@nowhere.com> wrote:
> On Mon, 26 Jul 2010 08:42:11 -0100, Parmenides wrote:
> > * *Inline function has been introduced in GNU and C99. Conceptually, a
> > * *inline funciton can be expaned at the point where it is called. But,
> > * *a macro can do this kind of job logically, though its expansion
> > * *occurs at souce level. Therefore, I wonder why bother introducing
> > * *the concept of inline function.
>
> Because macros are horrible. Macro "calls" look like function calls, but
> they often behave somewhat differently unless you put a lot of effort into
> making them behave like functions (wrapping the body in "do {} while(0)",
> copying the arguments to local variables to prevent repeated evaluation,
> etc).
>
> As a general rule, if you find yourself writing macros which are complex
> enough to require multiple lines, you should seriously reconsider your
> approach.
It probably is a good general rule, but I believe there are legitimate
exceptions. A few weeks ago I gritted my teeth and wrote several
complex multi-line macros. I felt it was justified because I wanted
minor variations of the same code to operate on different types of
variable within a loop in performance-critical functions. One of the
macro arguments was the type of data to operate upon.
Even if all the types are known, not all C compilers support 'inline'
and some of those which do support it have nasty bugs (as I discovered
last weekend).
--
Christopher Bazley
|