Inline Functions have numerous advantages over macros
but is there any scenario when macros can prove advantageous over inline functions?
Inline Functions have numerous advantages over macros
but is there any scenario when macros can prove advantageous over inline functions?
Standard C89 C doesn't have inline functions, so the question is moot.
C99 does have inline functions, and no I can't think of a good reason for using a function-like macro in place of an inline function.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I agree that inline functions for long macros is the best way. By the way, most modern compilers don't actually NEED the inline keyword to inline a function. Both MS and GCC compilers of recent versions will assuming sufficient optimization is enabled, inline functions even without this, assuming the compiler thinks it's a good idea to do
So, even with older standards, there not a desperate need to use macros just to get some code inlined.
However, the compiler will under some circumstances NOT want to inline some code, and a way to circumvent this is to use a macro but it really isn't a good way. (Unless of course the compiler supports something like "Forceinline").
Big macros is definitely a bad idea, because they are generally undebuggable - you can't step through each line of a macro, just the whole lot (or the assembler).
--
Mats
The best example of Brewbuck's case is when you include the same header file twice, changing the macros used in that headerfile inbetween. Naughty, but sometimes very effective.
--
Mats