当前位置:网站首页>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 .
边栏推荐
- Codeforces Round #716 (Div. 2) D. Cut and Stick
- Introduction to memory layout of FVP and Juno platforms
- Pointnet++的改进
- High precision subtraction
- PC寄存器
- 剑指 Offer 05. 替换空格
- Sword finger offer 53 - I. find the number I in the sorted array
- Configuration and startup of kubedm series-02-kubelet
- Palindrome (csp-s-2021-palin) solution
- Little known skills of Task Manager
猜你喜欢
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
SAP method of modifying system table data
Sword finger offer 04 Search in two-dimensional array
剑指 Offer 05. 替换空格
用STM32点个灯
Binary search basis
YOLOv5-Shufflenetv2
[speed pointer] 142 circular linked list II
[to be continued] [UE4 notes] L1 create and configure items
Sword finger offer 53 - I. find the number I in the sorted array
随机推荐
【实战技能】非技术背景经理的技术管理
In this indifferent world, light crying
Pointnet++的改进
Binary search basis
Sword finger offer 53 - I. find the number I in the sorted array
卷积神经网络——卷积层
浅谈JVM(面试常考)
Daily question - longest substring without repeated characters
网络工程师考核的一些常见的问题:WLAN、BGP、交换机
第六章 数据流建模—课后习题
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
SSH password free login settings and use scripts to SSH login and execute instructions
剑指 Offer 05. 替换空格
Reader writer model
Haut OJ 1321: mode problem of choice sister
To be continued] [UE4 notes] L4 object editing
注解与反射
Fried chicken nuggets and fifa22
YOLOv5添加注意力機制
Codeforces Round #716 (Div. 2) D. Cut and Stick