C++ General: What are the differences between inline functions and macros?

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

C++ General: What are the differences between inline functions and macros?

Q: What are inline functions good for?

A: Inline functions are best used for small functions such as accessing private data members. The main purpose of these inline functions (usually one or two lines of code) is to return state information about objects; short functions are sensitive to the overhead of function calls. Longer functions spend proportionately less time in the calling/returning sequence and benefit less from inlining. An inline function typically saves the overhead of:

Function calls (including parameter passing and placing the object's address on the stack)

Preservation of caller's stack frame

New stack-frame setup

Return-value communication

Old stack-frame restore

Return

The inline and __inline specifiers instruct the compiler to insert a copy of the function body into each place the function is called. The insertion (called "inline expansion" or "inlining") occurs only if the compiler's cost/benefit analysis show it to be profitable. Inline expansion alleviates the function-call overhead at the potential cost of larger code size.

Because there is no guarantee that an inline function will actually be inlined, you can bypass the compiler optimization by using the __forceinline keyword. This is Microsoft specific.

Originally Posted by MSDN

The __forceinline keyword overrides the cost/benefit analysis and relies on the judgment of the programmer instead. Exercise caution when using __forceinline. Indiscriminate use of __forceinline can result in larger code with only marginal performance gains or, in some cases, even performance losses (due to increased paging of a larger executable, for example).

However, this still does not force the compiler to actually inline every function you declared that way...

Originally Posted by MSDN

The compiler treats the inline expansion options and keywords as suggestions. There is no guarantee that functions will be inlined. You cannot force the compiler to inline a particular function, even with the __forceinline keyword.

Q: Iím confused about the use of inline functions and macros. What is the difference between them?

A: Inline functions are similar to macros because they both are expanded at compile time, but the macros are expanded by the preprocessor, while inline functions are parsed by the compiler. 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.

Q: When should I use macro and when inline functions?

A: Besides the difference already pointed out, you also must have in mind that because macros are expanded at pre-compile time, you cannot use them for debugging, but you can use inline functions.

In this example you can put a breakpoint in foo::maxim and step into the method, though it is an inline function. (if maxim was defined in the body of the foo class the keyword inline would have not been necessary) However, because macros are expanded before compilation actually starts, you cannot do that with the macro max. It is expanded at pre-compile time to:

you can see that it uses __FILE__ and __LINE__ to indicate the source file and the line where the assertion failed.

The other macro that I've mention, TRACE, sends the specified string to the debugger of the current application (just as ATLTRACE2 which has the same behaviour).

By mentioning just these macros doesn't mean they are the only useful macros. They are just some examples of useful macros. You should use macros when you cannot do something with an inline function or where the code will be more clean (maintainable) with the use of macros.