当前位置:网站首页>Scope of inline symbol
Scope of inline symbol
2022-07-05 05:34:00 【Raise items】
Compile time , Declare as inline The symbol of is expanded at the call , Reduce runtime overhead , At the same time, increase the size of the executable .
This article is about inline
The symbol of Scope .
Catalog
Example 1
a.cpp
#include <iostream>
inline int f()
{
return 10;
}
inline int g = 100;
void fa()
{
int a = f();
std::cout << a << std::endl;
std::cout << g << std::endl;
}
b.cpp
#include <iostream>
int f();
extern int g;
void fa();
void fb()
{
int b = f();
std::cout << b << std::endl;
std::cout << g << std::endl;
}
int main()
{
fa();
fb();
return 0;
}
Running results :
Look like extern
Scoped
Example 2
a.cpp
unchanged
#include <iostream>
inline int f()
{
return 10;
}
inline int g = 100;
void fa()
{
int a = f();
std::cout << a << std::endl;
std::cout << g << std::endl;
}
b.cpp
Define the same name inline Symbol
#include <iostream>
inline int f()
{
return 20;
}
inline int g = 200;
void fa();
void fb()
{
int b = f();
std::cout << b << std::endl;
std::cout << g << std::endl;
}
int main()
{
fa();
fb();
return 0;
}
Running results :
If it's normal extern Symbol , It should be reported redefinition Of link error , And there's no one here . In the whole project, only 1 individual f and 1 individual g, but f and g The value of is equal to Link order of . The latter link is ignored by the first link .
Example 3
a.cpp
The symbol in is declared as static
#include <iostream>
static inline int f()
{
return 10;
}
static inline int g = 100;
void fa()
{
int a = f();
std::cout << a << std::endl;
std::cout << g << std::endl;
}
b.cpp
unchanged
#include <iostream>
inline int f()
{
return 20;
}
inline int g = 200;
void fa();
void fb()
{
int b = f();
std::cout << b << std::endl;
std::cout << g << std::endl;
}
int main()
{
fa();
fb();
return 0;
}
Running results :
It is recognized in the project 2 individual f and 2 individual g, One is extern Scoped , One is static Scoped .
Conclusion
inline The symbolic scope of the modifier is extern
Of , But different from ordinary extern Symbol . In a project It is allowed to define multiple identical inline Symbol , Although it can be compiled , But after the link , Every inline Symbol Only one value will be retained .
So in a project ,inline Embellishment symbols should only
( Think of it as an ordinary extern Symbol ), Avoid quotation confusion .
边栏推荐
- 【Jailhouse 文章】Jailhouse Hypervisor
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- Little known skills of Task Manager
- A misunderstanding about the console window
- Add level control and logger level control of Solon logging plug-in
- Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
- Solution to the palindrome string (Luogu p5041 haoi2009)
- Codeforces round 712 (Div. 2) d. 3-coloring (construction)
- Mysql database (I)
- Introduction to tools in TF-A
猜你喜欢
挂起等待锁 vs 自旋锁(两者的使用场合)
智慧工地“水电能耗在线监测系统”
Binary search basis
[to be continued] [UE4 notes] L2 interface introduction
[depth first search] 695 Maximum area of the island
[article de jailhouse] jailhouse hypervisor
[merge array] 88 merge two ordered arrays
F - Two Exam(AtCoder Beginner Contest 238)
Chapter 6 data flow modeling - after class exercises
sync.Mutex源码解读
随机推荐
剑指 Offer 05. 替换空格
A misunderstanding about the console window
Reader writer model
Acwing 4300. Two operations
Acwing 4301. Truncated sequence
Codeforces Round #715 (Div. 2) D. Binary Literature
Gbase database helps the development of digital finance in the Bay Area
YOLOv5-Shufflenetv2
【Jailhouse 文章】Look Mum, no VM Exits
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
【Jailhouse 文章】Jailhouse Hypervisor
Maximum number of "balloons"
剑指 Offer 05. 替换空格
Sword finger offer 04 Search in two-dimensional array
YOLOv5-Shufflenetv2
Detailed explanation of expression (csp-j 2021 expr) topic
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
Codeforces Round #716 (Div. 2) D. Cut and Stick
[article de jailhouse] jailhouse hypervisor
Palindrome (csp-s-2021-palin) solution