Inline functions vs Preprocessor macros

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

How does an inline function differ from a preprocessor macro?

14条回答
  •  深忆病人
    2020-11-22 13:31

    First, the preprocessor macros are just "copy paste" in the code before the compilation. So there is no type checking, and some side effects can appear

    For example, if you want to compare 2 values:

    #define max(a,b) ((a

    The side effects appear if you use max(a++,b++) for example (a or b will be incremented twice). Instead, use (for example)

    inline int max( int a, int b) { return ((a

提交回复
热议问题