tags:

views:

60

answers:

1

Possible Duplicate:
Inline functions vs Preprocessor macros

what is the use of inline function and how it works? Are inline and macro different?

+1  A: 

An inline function evaluates it arguments the same way a function does (or at least you can think of it that way). That prevents the typical macro-errors to occurs. On the other hand, by stating that a function is inline you hint the compiler to avoid the function call and just insert the instructions inplace in the code (just like a macro).

So in short, it's safer than a macro, but just a hint to the compiler. The compiler isn't forced to avoid the function call (at least if I recall it correctly).

By the way, example of a "typical macro error":

#define SQUARE(a) (a*a)

int x = 10;
int square = SQUARE(++x); // this turns out to be 11 * 12, instead of 11 * 11

An inline function would have performed 11 * 11 instead.

Jakob
Actually the result here is undefined.
Martin York