tags:

views:

1085

answers:

11

How does an inline function differ from a preprocessor macro?

+4  A: 

The key difference is type checking. The compiler will check whether what you pass as input values is of types that can be passed into the function. That's not true with preprocessor macros - they are expanded prior to any type checking and that can cause severe and hard to detect bugs.

Here are several other less obvious points outlined.

sharptooth
+1  A: 

A inline functuion behaves syntactically just like a normal function, providing type safety and a scope for function local variables and access to class-members if it is a method. Also when calling inline methods you must adhere to private/protected restrictions.

heeen
+2  A: 

An inline function will maintain value semantics, whereas a preprocessor macro just copies the syntax. You can get very subtle bugs with a preprocessor macro if you use the argument multiple times - for example if the argument contains mutation like "i++" having that execute twice is quite a surprise. An inline function will not have this problem.

Michael Donohue
+14  A: 

Preprocessor macros are just substitution patterns applied to your code. They can be used almost anywhere in your code because they are replaced with their expansions before any compilation starts.

Inline functions are actual functions whose body is directly injected into their call site. They can only be used where a function call is appropriate.

Now, as far as using macros vs. inline functions in a function-like context, be advised that:

  • Macros are not type safe, and can be expanded regardless of whether they are syntatically correct - the compile phase will report errors resulting from macro expansion problems.
  • Macros can be used in context where you don't expect, resulting in problems
  • Macros are more flexible, in that they can expand other macros - whereas inline functions don't necessarily do this.
  • Macros can result in side effects because of their expansion, since the input expressions are copied wherever they appear in the pattern.
  • Inline function are not always guaranteed to be inlined - some compilers only do this in release builds, or when they are specifically configured to do so. Also, in some cases inlining may not be possible.
  • Inline functions can provide scope for variables (particularly static ones), preprocessor macros can only do this in code blocks {...}, and static variables will not behave exactly the same way.
LBushkin
Inline function are not always guaranteed to be inlined: Because the compiler will not inline if doing so will generate slower code etc. The compiler does a lot of analysis that the Engineer can not and does the correct thing.
Martin York
I believe that recursive functions are another example where most compilers ignore inlining.
LBushkin
Thank u sir. U have explained the differences vividly. Thank u again.
Subodh
+1  A: 

Macros are ignoring namespaces. And that makes them evil.

Kirill V. Lyadvinsky
+7  A: 

First, the preprocessor macros are just "copy paste" in the code before the compilation. So there is no type checking, and some side effects can appear

For example, if you want to compare 2 values:

#define max(a,b) ((a<b)?b:a)

The side effects appear if you use max(a++,b++) for example (a or b will be incremented twice). Instead, use (for example)

inline int max( int a, int b) { return ((a<b)?b:a); }
ThibThib
A: 

In GCC (I'm not sure about others), declaring a function inline, is just a hint to the compiler. It is still up to the compiler at the end of the day to decide whether or not it includes the body of the function whenever it is called.

The difference between in-line functions and preprocessor macros is relatively large. Preprocessor macros are just text replacement at the end of the day. You give up a lot of the ability for the compiler to perform checking on type checking on the arguments and return type. Evaluation of the arguments is much different (if the expressions you pass into the functions have side-effects you'll have a very fun time debugging). There are subtle differences about where functions and macros can be used. For example if I had:

#define MACRO_FUNC(X) ...

Where MACRO_FUNC obviously defines the body of the function. Special care needs to be taken so it runs correctly in all cases a function can be used, for example a poorly written MACRO_FUNC would cause an error in

if(MACRO_FUNC(y)) {
 ...body
}

A normal function could be used with no problem there.

Falaina
Thank u sir/madam for your answer. You have explained it vividly.
Subodh
A: 

To add another difference to those already given: you can't step through a #define in the debugger, but you can step through an inline function.

RichieHindle
A: 

From the perspective of coding, an inline function is like a function. Thus, the differences between an inline function and a macro are the same as the differences between a function and a macro.

From the perspective of compiling, an inline function is similar to a macro. It is injected directly into the code, not called.

In general, you should consider inline functions to be regular functions with some minor optimization mixed in. And like most optimizations, it is up to the compiler to decide if it actually cares to apply it. Often the compiler will happily ignore any attempts by the programmer to inline a function, for various reasons.

Brian
A: 

inline functions are similar to macros (because the function code is expanded at the point of the call at compile time), inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor. As a result, there are several important differences:

  • Inline functions follow all the protocols of type safety enforced on normal functions.
  • Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration.
  • Expressions passed as arguments to inline functions are evaluated once.
  • In some cases, expressions passed as arguments to macros can be evaluated more than once. http://msdn.microsoft.com/en-us/library/bf6bf4cf.aspx

  • macros are expanded at pre-compile time, you cannot use them for debugging, but you can use inline functions.

-- good article: http://www.codeguru.com/forum/showpost.php?p=1093923&amp;postcount=1

;

Ahmad
Thank u sir for your answer.
Subodh
+1  A: 

http://www.geekinterview.com/question_details/23831 has some good points.

Lazer