1. Inline function In C++ we usually define the following function to find the maximum value of two integers: Copy code The code is as follows: int max(int a, int b) { return a > b ? a : b; } The benefits of defining a function for such a small operation are: ① It is much easier to read and understand the call to the function max than to read an equivalent conditional expression and interpret its meaning ② If any modifications need to be made, it is much easier to modify the function than to find and modify each equivalent expression ③ Using functions can ensure uniform behavior, and each test is guaranteed to be implemented in the same way ④ Functions can be reused without having to rewrite code for other applications Although there are so many benefits, there is a potential disadvantage of writing it as a function: calling a function is much slower than solving an equivalent expression. On most machines, calling a function requires a lot of work: registers must be saved before the call and restored when returning, actual parameters must be copied, and the program must also switch to a new location to execute. C++ supports inline functions, whose purpose is to improve the execution efficiency of functions. You can specify a function as an inline function by putting the keyword in front of the function definition (note that it is a definition rather than a declaration, which will be discussed below). An inline function is usually expanded "inline" at each call point in the program. Suppose we define max as an inline function: Copy code The code is as follows: inline int max(int a, int b) { return a > b ? a : b; } Then call: cout< b ? a : b)<b ? a : b; } Main.cpp : Copy the code The code is as follows: #include
#include "A.h"
using namespace std;
inline int A::max()
{
return a > b ? a : b;
}
int main()
{
A a(3, 5);
cout<
Inline functions are rarely used in MCU programming. Generally, they are used in some short functions on ARM, and others don’t pay much attention to them. Thanks for sharing. I have studied them in depth. Thank you!
Inline functions are rarely used in MCU programming. Generally, they are used in some short functions on ARM, and others don’t pay much attention to them. Thanks for sharing. I have studied them in depth. Thank you!
Details
Published on 2018-9-12 08:38
Inline functions are rarely used in MCU programming. Generally, they are used in some short functions on ARM, and others don’t pay much attention to them. Thanks for sharing. I have studied them in depth. Thank you!