当前位置:网站首页>Macros, functions, and inline functions
Macros, functions, and inline functions
2022-06-29 09:07:00 【Flying fat Da Miao】
macro , Functions and inline functions
The difference between a macro and a function :
1,
Macros are simple string substitutions ( Data types are not included ).
Function parameters are passed with data types .
2,
Macro parameter substitution is handled directly without calculation .
A function call passes the value of an argument to a formal parameter ( Need to be calculated first )
3,
Macro replacement is performed before compilation , That is, use macro body instead of macro name , And then compile .
Function is called after compilation .
therefore : Macros take up compilation time , Functions take up execution time .
4,
Macro parameters do not occupy memory space ( It's just a string substitution )
The parameter transfer of function call is the information transfer between concrete variables , Formal parameters as local variables must occupy memory .
5,
Macro replacement does not require time and space .
A function needs some time and space to be called , The system needs to keep the scene when calling functions , Then go to the executed function to execute , Call complete , Then return the main function , Restore the scene at this time .
Defects of macro functions :
(1) Brackets must be added to ensure the completeness of the operation
#define add(x,y) x+y
int main()
{
int a = 1;
int b = 2;
int ret = add(a,b)*10;
cout<<ret<<endl;
// The output is not 30, It is 21.
// Because the macro function is just a simple string replacement , Direct will x+y Take it down , Because there is no parenthesis , indicating contrast b The first and 10 And we did the math .
}
(2) Even with parentheses , Some operations are still not as expected .
#define compare(a,b) (((a)<(b))?(a):(b)) // All the brackets that can be added are added
int main()
{
int a = 10;
int b = 20;
int ret = compare(++a,b);// expect : If a Self augmentation is still not complete b Big , So return a Result , Otherwise it returns b Result .
cout<<ret<<endl;// The expected result is 11, But the result is 12
// Because the operation is (((++a)<(b))?(++a):(b)), Two operations were performed , So is 12.
// This is the argument that the macro function replaces is the string , Normal functions pass values ( Calculated value ).
}
Inline function :
Definition
Itself is a real function , The above defects will not occur , Have all the behavior of a normal function . The only difference from the function is that it will be expanded like a macro in the appropriate place . Recall the advantages of macro functions : There is no need to stack in and out , Directly replace the string , Direct running source code , Space for time . The inline function just solves the defect of macro function and inherits its advantages .
Add... Before ordinary functions inline Keyword to call it an inline function
Be careful : The body of the function must be combined with the declaration , Otherwise, the compiler is treated as a normal function . The declaration and implementation of functions must be accompanied by keywords inline Is an inline function .
Be careful : The function system in the class automatically adds hidden inline function , Because the functions in the class are generally short , This is suitable for running the source code directly , So there will be no time overhead , Save time , Increase the running speed .
Inline functions and compilers
Following conditions , The compiler will not directly handle the inline function source code you write .
1, There can't be any form of loop statement .
2, There cannot be too many conditional judgments .
3, The function body cannot be too large ( There is no clear definition of how big ).
4, Cannot address a function ( The inline function itself has no address , What runs directly is the source code , No function entry , There is no point in taking an address ).
therefore : Inlining is just a suggestion for a compiler , The compiler does not necessarily accept this advice . If you don't declare a function as an inline function , The compiler may also treat this function as an inline function . A good compiler will make small , Simple function inline .
————————————————
Copyright notice : This paper is about CSDN Blogger 「 It's Xiaoming 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq_51399192/article/details/122738397
边栏推荐
- Core development board & debugger
- Official reply on issues related to the change of children's names after parents' divorce
- 记微信小程序setData动态修改字段名
- JS to obtain basic information about the width and height of an image or Base64
- C# 语音端点检测(VAD)实现过程分析
- 今天让你知道PMP考试通过率达97%,可信不可信
- H5软键盘问题
- ServerApp.iopub
- 记微信小程序分享代码片段
- MYSQL行转列例子
猜你喜欢
随机推荐
mongoDB 持久化
MYSQL虚拟列
H5 soft keyboard problem
微信小程序搜索关键字高亮和ctrl+f搜索定位实现
微信小程序判断url的文件格式
Analysis of c voice endpoint detection (VAD) implementation process
uni-app获取当前页面路由url
Verilog 大小端以及 +:使用
训练查看(问题暂存)
js轮播图观后重做(较长的完整版,可运行)
递归方法 rbac菜单层级显示 无限极分类
ActiveMQ message component publish subscribe redelivery message redelivery
2022第六季完美童模 清远赛区 海选赛圆满落幕
在 golang 中是如何对 epoll 进行封装的?
Handwriting Redux thunk
Pointnet的网络学习
(III) encoder self attention mask
DevOps到底是什么意思?
Verilog splicing operation symbol
laravel 8 实现 订单表按月份水平分表








