Inline functions vs Preprocessor macros

后端 未结 14 2120
一个人的身影
一个人的身影 2020-11-22 12:37

How does an inline function differ from a preprocessor macro?

相关标签:
14条回答
  • 2020-11-22 13:38

    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.

    0 讨论(0)
  • 2020-11-22 13:41

    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

    ;

    0 讨论(0)
提交回复
热议问题