Inline functions vs Preprocessor macros

2018-12-31 18:38发布

How does an inline function differ from a preprocessor macro?

13条回答
春风洒进眼中
2楼-- · 2018-12-31 19:17

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&postcount=1

;

查看更多
余欢
3楼-- · 2018-12-31 19:18

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.

查看更多
倾城一夜雪
4楼-- · 2018-12-31 19:18

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.

查看更多
ら面具成の殇う
5楼-- · 2018-12-31 19:21

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); }
查看更多
若你有天会懂
6楼-- · 2018-12-31 19:25

Macros are ignoring namespaces. And that makes them evil.

查看更多
余欢
7楼-- · 2018-12-31 19:25

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.

查看更多
登录 后发表回答