当前位置:网站首页>Can I compile the header file and source file of the template separately
Can I compile the header file and source file of the template separately
2022-04-23 14:05:00 【JokerYourMemory】
Strictly speaking , no way , However, this problem can be solved by ingenious means .
First, why not : Cite examples from this article Why? C++ The compiler cannot support separate compilation of templates ,
//-------------test.h----------------//
template<class T>
class A
{
public:
void f(); // This is just a statement
};
//---------------test.cpp-------------//
#include”test.h”
template<class T>
void A<T>::f() // Template implementation
{
…//do something
}
//---------------main.cpp---------------//
#include”test.h”
int main()
{
A<int> a;
f(); // #1
}
To put it bluntly It's a function template 、 Class template , Instantiate as a function definition at compile time ( Whether it can be instantiated depends on whether the compiler can make the template instance come out The sentence of ). The cross file function call is in link I'll find it when I need it After instantiation Function of . Because the header file is only used as a place to declare template classes or functions . Corresponding CPP Just as an implementation place , Then its instantiation will not be triggered , That's it CPP Corresponding .OBJ There is no specific function information in the file . This is a problem .
Then how to solve it ?
Two ways of thinking , I didn't think of anything else for the time being
The first is to let the template know main How the function calls his , Then you need to put the definition of the template in the header file , then main Functional cpp Just include it in the header file . But if there are multiple classes 、 It's all done in this way , Then the problem of repeated definitions will occur during compilation , Similar to my last article Can global variables be declared in the header file .
Another idea is , Give Way .h share .cpp Information about , That is to say .h add #include<.cpp>, But this kind of .h contain .cpp Is your approach rigorous .
There's another way , Since the link is not possible, it is because there is no call to generate class instances during compilation , So can you call this class or function through an independent function To generate one . It's fine too , But it still faces the risk of repeated definition of the first approach .
版权声明
本文为[JokerYourMemory]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401091135.html
边栏推荐
- VsCode-Go
- Autumn recruitment in 2021, salary ranking No
- 微信小程序获取登录用户信息、openid和access_token
- MySQL 修改主数据库
- Prediction of tomorrow's trading limit of Low Frequency Quantization
- 帆软中根据分类进行汇总
- Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
- New关键字的学习和总结
- _模_板_
- 服务器日志分析工具(识别,提取,合并,统计异常信息)
猜你喜欢
As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
scikit-learn构建模型的万能模板
STM32 learning record 0007 - new project (based on register version)
室内外地图切换(室内基于ibeacons三点定位)
Nacos+AspnetCore+Ocelot实战编码
Universal template for scikit learn model construction
帆软中需要设置合计值为0时,一整行都不显示的解决办法
使用Postman进行Mock测试
Use of WiFi module based on wechat applet
分库分表 & ShardingSphere
随机推荐
9月8日,临去松山湖的前夜
Restful WebService和gSoap WebService的本质区别
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
生产环境——
L2-024 tribe (25 points)
Decimal 格式化小数位/DateTime 转换处理
nodejs通过require读取本地json文件出现Unexpected token / in JSON at position
json反序列化匿名数组/对象
关于stream流,浅记一下------
New关键字的学习和总结
Three point positioning based on ibeacons (wechat applet)
linux安装mysql后修改密码
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
Idea控制台乱码解决
基于ibeacons三点定位(微信小程序)
Chapter 15 new technologies of software engineering
基于Ocelot的gRpc网关
Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)
VsCode-Go
微信小程序 input隐藏和不可操作的设置