What's the difference in practice between inline and #define?

后端 未结 5 1687
遇见更好的自我
遇见更好的自我 2021-02-13 06:47

As the title says; what\'s the difference in practice between the inline keyword and the #define preprocessor directive?

5条回答
  •  一生所求
    2021-02-13 07:28

    Function-like macros give you absolutely zero sanity checking at the place where they're defined. When you screw up with a macro, it'll work fine in one place and be broken somewhere else, and you won't know why until you've lost several hours of work/sleep time. Function-like macros do not operate on data, they operate on source code. Sometimes this is good, for example when you want reusable debug statements that use FILE and LINE builtins, but most of the time, it can be done just as well with an inline function, which is checked for syntax at the point of definition just like any other function.

提交回复
热议问题