Re: Why bother introducing inline function

Discussion in 'C Programming' started by Nobody, Jul 27, 2010.

  1. Nobody

    Nobody Guest

    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.
    Nobody, Jul 27, 2010
    #1
    1. Advertising

  2. Nobody

    Guest

    On Jul 27, 11:05 am, Nobody <> 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
    , Jul 27, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. xyZed
    Replies:
    3
    Views:
    325
    Charles Banas
    Apr 20, 2004
  2. Travis Newbury

    why bother replying at all?

    Travis Newbury, Jun 2, 2005, in forum: HTML
    Replies:
    9
    Views:
    468
    Hywel Jenkins
    Jun 3, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,754
    Smokey Grindel
    Dec 2, 2006
  4. Eric Sosman

    Re: Why bother introducing inline function

    Eric Sosman, Jul 26, 2010, in forum: C Programming
    Replies:
    4
    Views:
    322
    Richard Bos
    Aug 5, 2010
  5. Billy Mays

    Re: Why bother introducing inline function

    Billy Mays, Jul 26, 2010, in forum: C Programming
    Replies:
    14
    Views:
    486
    Tim Rentsch
    Aug 1, 2010
Loading...

Share This Page