Thread: Macros vs Inline Functions

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Macros vs Inline Functions

    Inline Functions have numerous advantages over macros

    but is there any scenario when macros can prove advantageous over inline functions?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vb.bajpai View Post
    but is there any scenario when macros can prove advantageous over inline functions?
    A macro can be redefined -- an inline function can't be. There are some obscure, rare cases where this is useful.

    For the same reason, #define constants are sometimes preferable to defining const variables, because they can be overridden at compile-time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inline empty polymorphic functions
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-16-2009, 12:12 PM
  2. Inline functions and passing by value
    By gentinex in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2006, 06:53 AM
  3. Macros Vs Inline functions
    By passionate_guy in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:52 AM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:37 PM