当前位置:网站首页>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 .
边栏推荐
- [sum of two numbers] 169 sum of two numbers II - enter an ordered array
- sync.Mutex源码解读
- To the distance we have been looking for -- film review of "flying house journey"
- R语言【数据集的导入导出】
- 记录QT内存泄漏的一种问题和解决方案
- 剑指 Offer 09. 用两个栈实现队列
- 个人开发的渗透测试工具Satania v1.2更新
- kubeadm系列-01-preflight究竟有多少check
- [to be continued] [UE4 notes] L2 interface introduction
- 剑指 Offer 58 - II. 左旋转字符串
猜你喜欢

【Jailhouse 文章】Look Mum, no VM Exits

CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five

YOLOv5添加注意力機制

利用HashMap实现简单缓存

object serialization

Yolov5 ajouter un mécanisme d'attention

【Jailhouse 文章】Jailhouse Hypervisor

sync. Interpretation of mutex source code

Pointnet++学习

Binary search basis
随机推荐
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Sword finger offer 04 Search in two-dimensional array
Palindrome (csp-s-2021-palin) solution
[depth first search] 695 Maximum area of the island
剑指 Offer 06.从头到尾打印链表
[to be continued] [depth first search] 547 Number of provinces
Pointnet++学习
利用HashMap实现简单缓存
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
【实战技能】如何做好技术培训?
CF1634E Fair Share
The number of enclaves
A problem and solution of recording QT memory leakage
Sword finger offer 53 - ii Missing numbers from 0 to n-1
High precision subtraction
卷积神经网络简介
Improvement of pointnet++
Yolov5 adds attention mechanism
Haut OJ 1321: mode problem of choice sister
AtCoder Grand Contest 013 E - Placing Squares