How does an inline function differ from a preprocessor macro?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
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.
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.
Preprocessor macros are just substitution patterns applied to your code. They can be used almost anywhere in your code because they are replaced with their expansions before any compilation starts.
Inline functions are actual functions whose body is directly injected into their call site. They can only be used where a function call is appropriate.
Now, as far as using macros vs. inline functions in a function-like context, be advised that:
The Inline function are expanded by the compiler where as the macros are expanded by the Preprocessor, which is mere textual substitution.Hence
There is no type checking during macro invocation while type checking is done during function call.
Undesired results and inefficiency may occur during macro expansion due to reevaluation of arguments and order of operations. For example
would result in
The macro arguments are not evaluated before macro expansion
The return keyword cannot be used in macros to return values as in the case of functions.
Inline functions can be overloaded
The tokens passed to macros can be concatenated using operator ## called Token-Pasting operator .
Macros are generally used for code reuse where as inline functions are used to eliminate the time overhead (excess time) during function call(avoiding a jump to a subroutine).
Macros are typically faster than functions as they don’t involve actual function call overhead.
Some Disadvantages of macros: There is no type checking.Difficult to debug as they cause simple replacement.Macro don’t have namespace, so a macro in one section of code can affect other section. Macros can cause side effects as shown in above CUBE() example.
Macros are usually one liner. However, they can consist of more than one line.There are no such constraints in functions.
The key difference is type checking. The compiler will check whether what you pass as input values is of types that can be passed into the function. That's not true with preprocessor macros - they are expanded prior to any type checking and that can cause severe and hard to detect bugs.
Here are several other less obvious points outlined.