inline function vs #define [duplicate]

假如想象 提交于 2019-12-25 04:02:28

问题


Possible Duplicate:
Inline functions vs Preprocessor macros

In C++ the inline function qualifier basically replaces the function as a #define directive, rather than making the function being called over and over again. Thus reducing overhead time, but at the same time increasing program size.

If my understanding of inline function is correct, what is the different, of inline and #define?


回答1:


  • inline is only a hint, which the compiler is free to ignore

  • #defines aren't a compiler-level feature—they're substitutions, not functions

    • In order to safely put multiple statements in a macro, you must wrap it in do { ... } while(0).
    • The parameters of a macro are re-evaluated every time they're used.
    • Names in the caller scope are accessible to the macro


来源:https://stackoverflow.com/questions/6402613/inline-function-vs-define

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!