Inline functions vs Preprocessor macros

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

How does an inline function differ from a preprocessor macro?

14条回答
  •  悲&欢浪女
    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

    ;

提交回复
热议问题