Article Directory
1. Summary of inline functions
Recall the inline functions introduced in the previous blog:
- Inline function compilation: When the C++ compiler compiles an inline function , it will directly insert the inline function body instruction into the position where the inline function is called;
- Inline requests will be rejected: use the inline keyword to modify ordinary functions and convert them into inline functions. The compiler may not agree to the inline request. If there are loop statements/ many conditional judgment statements/ large function body/ for functions Take the address operation/ declare the inline function separately, even if the inline inline function is written, the compiler will not agree to the inline request;
- Advantages of inline functions: Compared with ordinary functions, inline functions have the advantage of eliminating the overhead of pushing/jumping/returning when the function is called;
2. Inline function code example
1. Code example - common function execution analysis
call int c = fun1(++a, b); code, fun1 is a normal function;
When executed,
First, execute the self-increment of a, then variable a = 2;
Then, execute the fun1 ordinary function, pass in two parameters, pass a = 2, b = 3 as actual parameters into the ordinary function;
Finally, the ordinary function returns the smaller value of a = 2, b = 3, and assigns it to c = a = 2;
Therefore, the final result is: a = 2 , b = 3 , c = 2;
Code example - inline function execution analysis:
// 导入标准 io 流头文件 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;
// 声明内联函数 不会报错 程序能正常运行
// 但是不建议这样做
// 一旦像这样声明 内联函数
// 编译器 编译时 会拒绝该内联函数的 内联请求
// 将其作为普通函数处理
//inline int fun(int a, int b);
// 宏代码片段 : 获取 a 和 b 中较小的值
#define FUN(a, b) ((a) < (b) ? (a) : (b))
// 内联函数 : 获取 a 和 b 中较小的值
inline int fun(int a, int b)
{
return a < b ? a : b;
}
// 普通函数 : 获取 a 和 b 中较小的值
inline int fun1(int a, int b)
{
return a < b ? a : b;
}
int main() {
int a = 1;
int b = 3;
// 调用普通函数
// 此时先执行 a 的自增 a = 2
// 然后再执行函数 , 将 a = 2 , b = 3 传入
// 结果得到 a b 中较小的值 c = a = 2
int c = fun1(++a, b);
// 打印内联函数调用结果
printf("a = %d, b = %d, c = %d\n", a, b, c);
// 控制台暂停
system("pause");
return 0;
}
Results of the :
a = 2, b = 3, c = 2
请按任意键继续. . .
2. Code example - inline function execution analysis
call int c = fun(++a, b); code, fun is an inline function;
When executed,
First, execute the self-increment of a, then variable a = 2;
Then, execute the fun inline function, pass in two parameters, pass a = 2, b = 3 as actual parameters into the inline function;
Finally, the inline function returns the smaller value of a = 2, b = 3, and assigns it to c = a = 2;
Therefore, the final result is: a = 2 , b = 3 , c = 2;
The execution result of the inline function is the same as that of the ordinary function;
Code example - inline function execution analysis:
// 导入标准 io 流头文件 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;
// 声明内联函数 不会报错 程序能正常运行
// 但是不建议这样做
// 一旦像这样声明 内联函数
// 编译器 编译时 会拒绝该内联函数的 内联请求
// 将其作为普通函数处理
//inline int fun(int a, int b);
// 宏代码片段 : 获取 a 和 b 中较小的值
#define FUN(a, b) ((a) < (b) ? (a) : (b))
// 内联函数 : 获取 a 和 b 中较小的值
inline int fun(int a, int b)
{
return a < b ? a : b;
}
// 普通函数 : 获取 a 和 b 中较小的值
inline int fun1(int a, int b)
{
return a < b ? a : b;
}
int main() {
int a = 1;
int b = 3;
// 调用内联函数
// 此时先执行 a 的自增 a = 2
// 然后再执行函数 , 将 a = 2 , b = 3 传入
// 结果得到 a b 中较小的值 c = a = 2
int c = fun(++a, b);
// 打印内联函数调用结果
printf("a = %d, b = %d, c = %d\n", a, b, c);
// 控制台暂停
system("pause");
return 0;
}
Results of the :
a = 2, b = 3, c = 2
请按任意键继续. . .
3. Code example - macro code fragment execution analysis
Execute int c = FUN(++a, b); code, call macro code fragment;
The macro code fragment mechanically expands the macro in the precompilation stage, and the macro replacement is very mechanical;
The result of macro substitution is as follows: Replace ++a with the position of a in FUN(a, b),
((++a) < (b) ? (++a) : (b))
The execution process is as follows:
- First, execute ++a to become 2;
- Then, compare 2 < 3, and the return result is (++a) At this time, a needs to be incremented again to become 3;
- Finally, a = 3 returns, the minimum value is 3;
Code example - macro code fragment execution analysis:
// 导入标准 io 流头文件 其中定义了 std 命名空间
#include <iostream>
// 导入 std 命名空间
using namespace std;
// 声明内联函数 不会报错 程序能正常运行
// 但是不建议这样做
// 一旦像这样声明 内联函数
// 编译器 编译时 会拒绝该内联函数的 内联请求
// 将其作为普通函数处理
//inline int fun(int a, int b);
// 宏代码片段 : 获取 a 和 b 中较小的值
#define FUN(a, b) ((a) < (b) ? (a) : (b))
// 内联函数 : 获取 a 和 b 中较小的值
inline int fun(int a, int b)
{
return a < b ? a : b;
}
// 普通函数 : 获取 a 和 b 中较小的值
inline int fun1(int a, int b)
{
return a < b ? a : b;
}
int main() {
int a = 1;
int b = 3;
// 调用宏代码片段
// 机械的将宏展开 , 宏替换很机械
// 宏替换结果如下 : 将 ++a 替换到 FUN(a , b) 中 a 的位置
// ((++a) < (b) ? (++a) : (b))
// 首先 , 执行 ++a 变为 2 ,
// 然后 , 比较 2 < 3 , 返回结果是 (++a) 此时又要自增一次 a 变为 3
// 最后 , a = 3 返回 , 最小值为 3
int c = FUN(++a, b);
// 打印内联函数调用结果
printf("a = %d, b = %d, c = %d\n", a, b, c);
// 控制台暂停
system("pause");
return 0;
}
Results of the :
a = 3, b = 3, c = 3
请按任意键继续. . .