Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Why bother introducing inline function

Reply
Thread Tools

Re: Why bother introducing inline function

 
 
Nobody
Guest
Posts: n/a
 
      07-27-2010
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.

 
Reply With Quote
 
 
 
 
chrisbazley@bigfoot.com
Guest
Posts: n/a
 
      07-27-2010
On Jul 27, 11:05*am, Nobody <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why bother introducing inline function Eric Sosman C Programming 4 08-05-2010 12:46 PM
Re: Why bother introducing inline function Billy Mays C Programming 14 08-01-2010 02:56 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Use of inline function in inline function - is it allowed? TGOS C Programming 3 02-28-2005 10:49 AM
Function delcared inline but not defined inline Nish C Programming 4 10-08-2004 03:31 PM



Advertisments