当前位置:网站首页>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 .
边栏推荐
- [to be continued] [UE4 notes] L1 create and configure items
- Gbase database helps the development of digital finance in the Bay Area
- Pointnet++的改进
- Chapter 6 data flow modeling - after class exercises
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Using HashMap to realize simple cache
- 软件测试 -- 0 序
- [to be continued] [UE4 notes] L2 interface introduction
- [to be continued] [UE4 notes] L3 import resources and project migration
- 【实战技能】如何做好技术培训?
猜你喜欢
随机推荐
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
卷积神经网络——卷积层
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Maximum number of "balloons"
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Sword finger offer 09 Implementing queues with two stacks
[to be continued] [depth first search] 547 Number of provinces
Haut OJ 1241: League activities of class XXX
Solution to the palindrome string (Luogu p5041 haoi2009)
数仓项目的集群脚本
After setting up the database and website When you open the app for testing, it shows that the server is being maintained
个人开发的渗透测试工具Satania v1.2更新
In this indifferent world, light crying
[binary search] 34 Find the first and last positions of elements in a sorted array
[es practice] use the native realm security mode on es
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Reader writer model
Time complexity and space complexity
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
Haut OJ 1218: maximum continuous sub segment sum