当前位置:网站首页>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 .
边栏推荐
- [depth first search] 695 Maximum area of the island
- Control Unit 控制部件
- SAP-修改系统表数据的方法
- 个人开发的渗透测试工具Satania v1.2更新
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- Reflection summary of Haut OJ freshmen on Wednesday
- Talking about JVM (frequent interview)
- 全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
- Using HashMap to realize simple cache
猜你喜欢
随机推荐
CF1637E Best Pair
A problem and solution of recording QT memory leakage
Reader writer model
Developing desktop applications with electron
2020ccpc Qinhuangdao J - Kingdom's power
How many checks does kubedm series-01-preflight have
YOLOv5添加注意力机制
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
注解与反射
Sword finger offer 53 - I. find the number I in the sorted array
Haut OJ 1221: a tired day
Introduction to tools in TF-A
PC寄存器
AtCoder Grand Contest 013 E - Placing Squares
Sword finger offer 05 Replace spaces
Pointnet++的改进
Haut OJ 1357: lunch question (I) -- high precision multiplication
Drawing dynamic 3D circle with pure C language
卷积神经网络——卷积层
剑指 Offer 04. 二维数组中的查找






![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)

