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

后端 未结 5 1689
遇见更好的自我
遇见更好的自我 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:18

    Well, a multi-line #define is harder to write and edit than an inline function. You can define an inline function just like any normal function and can define variables without problems. Imagine that you want to call a code-block several times within another function, and that code block needs its own variables: it's easier to do with inline functions (yes, you can do so with #defines and do { ... } while (0); but it's something you have to think about).

    Also, with enabled debugging, you normally get a "simulated" stack-frame for inline functions which might make debugging easier sometimes (at least that's what you get when you compile/link with gcc's -g and debug with GDB, IIRC). You can place breakpoints inside your inline'd function.

    Apart from that the result should be almost identical, AFAIK.

提交回复
热议问题