当前位置:网站首页>03-1. Inline function, auto keyword, typeID, nullptr
03-1. Inline function, auto keyword, typeID, nullptr
2022-07-19 11:31:00 【The streets of Leningrad】
Preface : This article is mainly used for personal review , Pursue simplicity , Thank you for your reference 、 Communication and handling , It may be revised and improved in the future .
Because it's a personal review , There will be partial compression and omission .
One 、 Inline function
C Language in order to avoid small functions to establish stack frames , Macro functions are provided , It will be carried out in the pretreatment stage
since C Language has been solved , Why? C++ Also provide inline Function? ?( Disadvantages of macro functions )
1. Macro functions do not support debugging
2. Macro function syntax is complex , It's easy to make mistakes
3. Macro functions do not have type safety checks
C++ Small functions that are frequently called are recommended , Defined as inline, Will expand where called , No stack frame overhead
Can all functions be inlined ?
For example, here is a sort Function has 100 Line instruction , Yes 10 Place to call , How many instructions is the total ?
Yes 110 strip .
If it becomes an inline function , How many ?
100*10, Yes 1000 Orders .
What does it mean to have more instructions ?
More instructions means that the compiled executable program becomes larger , It will make the experience of the people who install the software worse , The memory consumption of the executing program becomes more
Conclusion : Small functions that are called frequently , The suggestion is defined as inline
Characteristics of inline functions :
1. inline It's a way of trading space for time , Save the cost of calling the function . So the code is long or has loops / Recursive functions are not suitable for inline functions .
2. inline Just a suggestion for the compiler , The compiler automatically optimizes , If defined as inline The function body of is too large or recursive , The compiler optimizes without inline .
3. inline Separation of declaration and definition is not recommended , Separation can lead to link errors . because inline Be unfolded , There is no function address , The link will not be found .
The root cause of link errors : Inline function has no address !!!
Two 、auto
Type declaration auto, According to a The type of automatic derivation b The type of
(auto*)malloc() This is not allowed
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int a = 10;
//int b = a;
auto b = a;// The type is declared as auto, According to a The type of automatic derivation b The type of
map<string, string> m;
//map<string, string>::iterator it = m.begin();
// Here we can base on m.begin() Automatic derivation it The type is map<string, string>::iterator
auto it = m.begin();
return 0;
}
auto There are several places that cannot be used :
1.auto Cannot be used as an argument to a function
auto Cannot be a parameter type , Because the compiler can't a The actual type of
void test(auto a) {}2.auto Cannot be used directly to declare arrays
void test() { int a[] = {1,2,3}; auto b[] = {4,5,6}; // Can you do this ? When creating an array ,auto b[0] = a[1]; auto a = 4.1,b = 4;// You can't either }3.auto In practice, the most common advantage usage is to talk about morality later C++11 New for loop , also lambda Expressions, etc
auto The real significance lies in supporting C++11 The new grammatical scope of for, The new syntax traverses , It's simpler , Array is ok , Containers can also

e Your name can be changed


Be careful , In this case, the scope cannot be used for
void test(int arr[])
{
for(auto& e : arr)
{
cout << e << endl;
}
} Range for Conditions of use
1. for The scope of loop iteration must be determined
For arrays , Is the range of the first and last element in the array ; For a class , Should provide begin and end Methods ,begin and end Namely for Scope of loop iteration . Be careful : There is a problem with the following code , because for The scope of is uncertain ,int array[] It is not an array , It's a pointer , So no
void TestFor(int array[])
{
for(auto& e : array)
cout<< e <<endl;
}2. iteration
The object of the ++ and == The operation of .( About iterators , I'll talk about it later )
3、 ... and 、nullptr and NULL
stay C++ We use nullptr, because C++ in NULL It's actually being define become 0 Of , Is such a macro , It's not a pointer
void f(int)
{
cout<<"f(int)"<<endl;
}
void f(int*)
{
cout<<"f(int*)"<<endl;
}
int main()
{
f(0);
f(NULL);
f((int*)NULL);
return 0;
} The purpose of the program is to pass f(NULL) Call the pointer version of f(int*) function , But because of NULL Defined as 0, Therefore, it is contrary to the original intention of the procedure .
stay C++98 in , Literal constants 0 It can be an integer number , It can also be a typeless pointer (void*) Constant , But the compiler by default
Think of it as an integer constant , If you want to use it as a pointer , It must be forced (void *)0.
边栏推荐
- The type of MySQL index (single column index, combined index, BTREE index, clustered index, etc.)
- 搭建OpenStack-M版的Cinder所碰到过的状况
- AT5147-[AGC036D]Negative Cycle【dp,模型转换】
- 解决邮件客户端QQ Mail及Thunderbird无法登入Outlook的问题
- synchronized锁升级
- To get to the bottom: Principle Analysis of Objective-C correlation attribute
- A simple output method of promise object to the result in nodejs (it is recommended to use the asynchronous ultimate scheme async+await)
- Mysql 自增id、uuid与雪花id
- 热议:老公今年已经34周岁想读博,以后做科研,怎么办?
- Introduction to virtualization troubleshooting
猜你喜欢

E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)

【PostgreSQL 】PostgreSQL 15对distinct的优化

Unity3d read mpu9250 example source code

XSS. haozi. Me brush questions

Evaluation method of machine learning model

开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?

Unity dropdown (editable, inputable) drop-down selection box with Text Association

Detailed explanation of MySQL show processlist

03-1、内联函数、auto关键字、typeid、nullptr

SPI service discovery mechanism
随机推荐
Introduction to common distributed locks
E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)
AT5147-[AGC036D]Negative Cycle【dp,模型转换】
【PostgreSQL 】PostgreSQL 15对distinct的优化
夢想CMS 前臺搜索SQL注入
Use and principle of ThreadLocal variable
性能优化之@Contended减少伪共享
Powercli script performance optimization
Codeforces - 587e (linear basis + segment tree + difference)
CodeForces - 587E(线性基+线段树+差分)
《MySQL DBA封神打怪之路》专栏学习大纲
Cmake常用命令(五)
Huawei machine test: number of continuous licensing
466-82(3、146、215)
Total number of blocking and waiting in jconsole thread panel (RPM)
Introduction to virtualization troubleshooting
Similarities and differences between OA system and MES system
Unity高版本退回低版本报错问题
cv02-roge矩阵,旋转向量 ,角度
早期单片机加密的一些方法 【评论区领取资料】