当前位置:网站首页>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.
边栏推荐
- Cmake common commands (V)
- 今日睡眠质量记录79分
- jconsole线程面板中的阻塞总数和等待总数(转)
- Detailed explanation of Euler angle, axis angle, quaternion and rotation matrix
- mysql show processlist 详解
- Daily question brushing record (26)
- A summary of C language pointer
- 学习笔记3--规划控制中的机器学习基本思想
- Conversion of unity3d model center point (source code)
- Basic operation of tree
猜你喜欢

Introduction of database lock, shared with InnoDB, exclusive lock

Today's sleep quality record 79 points

Powercli script performance optimization

Performance optimization @contented to reduce pseudo sharing

Unity high version returned low version error

A fastandrobust convolutionalneuralnetwork-based defect detection model inproductqualitycontrol-閱讀筆記

02-3、指针和引用的区别

Detailed explanation of MySQL show processlist

Unity高版本退回低版本报错问题

Four methods of traversing key value in map
随机推荐
公网连接MySQL实例的解决方案
Huawei machine test: number of continuous licensing
To get to the bottom: Principle Analysis of Objective-C correlation attribute
XSS.haozi.me刷题
SAP S4 material management inventory module mard database table reading technical details
Un modèle de détection par défaut basé sur le réseau neuronal évolutif rapide dans le contrôle de la qualité des produits - lire les notes
微服务上线规范
JVM钩子hooks函数
The difference between CPU load and CPU utilization
Introduction to sap appgyver
Robot development -- robot data summary
Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
MySQL autoincrement ID, UUID and snowflake ID
Basic operation of tree
Accident caused by the combination of annotation transaction + distributed lock at the read committed level -- disorder of activity purchase opportunities
ThreadLocal变量使用及原理
PowerCLI 脚本性能优化
Dream CMS foreground search SQL injection
机器人开发--常用仿真软件工具
常用getshell工具的下载