Inline functions vs Preprocessor macros

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

How does an inline function differ from a preprocessor macro?

相关标签:
14条回答
  • 2020-11-22 13:31

    From the perspective of coding, an inline function is like a function. Thus, the differences between an inline function and a macro are the same as the differences between a function and a macro.

    From the perspective of compiling, an inline function is similar to a macro. It is injected directly into the code, not called.

    In general, you should consider inline functions to be regular functions with some minor optimization mixed in. And like most optimizations, it is up to the compiler to decide if it actually cares to apply it. Often the compiler will happily ignore any attempts by the programmer to inline a function, for various reasons.

    0 讨论(0)
  • 2020-11-22 13:32

    To add another difference to those already given: you can't step through a #define in the debugger, but you can step through an inline function.

    0 讨论(0)
  • 2020-11-22 13:32

    In GCC (I'm not sure about others), declaring a function inline, is just a hint to the compiler. It is still up to the compiler at the end of the day to decide whether or not it includes the body of the function whenever it is called.

    The difference between in-line functions and preprocessor macros is relatively large. Preprocessor macros are just text replacement at the end of the day. You give up a lot of the ability for the compiler to perform checking on type checking on the arguments and return type. Evaluation of the arguments is much different (if the expressions you pass into the functions have side-effects you'll have a very fun time debugging). There are subtle differences about where functions and macros can be used. For example if I had:

    #define MACRO_FUNC(X) ...
    

    Where MACRO_FUNC obviously defines the body of the function. Special care needs to be taken so it runs correctly in all cases a function can be used, for example a poorly written MACRO_FUNC would cause an error in

    if(MACRO_FUNC(y)) {
     ...body
    }
    

    A normal function could be used with no problem there.

    0 讨论(0)
  • 2020-11-22 13:34

    inline functions will behave as a function call if there exists any iterative or recursive statement in it, so as to prevent repeated execution of instructions. Its quite helpful to save the overall memory of your program.

    0 讨论(0)
  • 2020-11-22 13:35

    An inline function will maintain value semantics, whereas a preprocessor macro just copies the syntax. You can get very subtle bugs with a preprocessor macro if you use the argument multiple times - for example if the argument contains mutation like "i++" having that execute twice is quite a surprise. An inline function will not have this problem.

    0 讨论(0)
  • 2020-11-22 13:36

    A inline functuion behaves syntactically just like a normal function, providing type safety and a scope for function local variables and access to class-members if it is a method. Also when calling inline methods you must adhere to private/protected restrictions.

    0 讨论(0)
提交回复
热议问题