如何在动态链接库dll/so中导出自定义的模板类template class | how to implement a template class with c++ and export in dll/so
本文首发于个人博客https://kezunlin.me/post/4ec4ae49/,欢迎阅读最新内容!
how to implement a template class with c++ and export in dll/so
Guide
questions
模板类必须在header中实现,而不能在cpp中实现,否则作为dll调用进行链接的时候回出错。
common solutions(Recommend)
implement template functions in header.
ThreadPool.h
class SHARED_EXPORT ThreadPool {
public:
static ThreadPool* Instance(size_t max_thread_pool_size);
~ThreadPool();
// Add new work item to the pool.
template<class F>
inline void Enqueue(F f)
{
io_service_.post(f);//sync, return immediately
}
void Free();
private:
static std::shared_ptr<ThreadPool> m_pInstance;
bool bfree;
ThreadPool(size_t size);
DISABLE_COPY_AND_ASSIGN(ThreadPool);
boost::thread_group workers_;
boost::asio::io_service io_service_;
boost::asio::io_service::work work_;
};
Seperate from headers
solutions 1
A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.
Foo.h
template <typename T>
struct Foo
{
void doSomething(T param);
};
#include "Foo.cpp" // here
Foo.cpp
template <typename T>
void Foo<T>::doSomething(T param)
{
//implementation
}
solutions 2
Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you'll need:
Foo.h
// no implementation
template <typename T> struct Foo { ... };
Foo.cpp
#include "Foo.h"
// implementation of Foo's methods
// explicit instantiations
template class Foo<int>;
template class Foo<float>;
// You will only be able to use Foo with int or float
// template void TestClass::templateFunction<int, int>(int, int);
Reference
- why-can-templates-only-be-implemented-in-the-header-file
- separate-c-template-headers-h-and
- Cpp_template_definitions_in_a_cpp_file_instead_of_header
History
- 20191012: created.
Copyright
- Post author: kezunlin
- Post link: https://kezunlin.me/post/4ec4ae49/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
如何在动态链接库dll/so中导出自定义的模板类template class | how to implement a template class with c++ and export in dll/so的更多相关文章
- ArcGIS api fo silverlight学习三(利用ElementLayer实现鼠标悬浮弹出自定义窗体)
接着上一节继续学习,本节主要是利用ElementLayer实现鼠标悬浮弹出自定义窗体 参考博文:http://www.cnblogs.com/luxiaoxun/p/3322218.html 一.新建 ...
- 实现android手机来电拦截系统页面弹出自定义页面特效
如何实现android手机来电拦截系统页面弹出自定义页面特效, 首先: 我们需要注册一个监听来电的广播PhoneStateReceiver 类:其次: 在onReceive里面我们获取an ...
- android高德地图网络路径实现自定义marker并点击弹出自定义窗口
android中使用地图的地方随处可见,今天记录一下网络路径生成自定义marker,点击标记弹出自定义的窗口(在这里使用的是高德地图) 在这里我们使用Grilde去加载网络图片,因为这个简直太方便了! ...
- 【WPF】右下角弹出自定义通知样式(Notification)——简单教程
原文:[WPF]右下角弹出自定义通知样式(Notification)--简单教程 1.先看效果 2.实现 1.主界面是MainWindow 上面就只摆放一个Button即可.在Button的点击事件中 ...
- MFC DLL 导出函数的定义方式
一直在鼓捣DLL,每天的工作都是调试一个一个的DLL,往DLL里面添加自己的代码,但是对于DLL一直不太了解啊!今天一查资料,才发现自己对于DLL编写的一些基本知识也不了解.要学习,这篇文章先总结DL ...
- DLL中导出STL模板类的问题
接上一篇. 上一篇的dll在编译过程中一直有一个警告warning C4251: ‘CLASS_TEST::m_structs’ : class ‘std::vector<_Ty>’ ne ...
- DLL模块例2:使用__declspec(dllexport)导出函数,extern "C"规范修饰名称,隐式连接调用dll中函数
以下内容,我看了多篇文章,整合在一起,写的一个例子,关于dll工程的创建,请参考博客里另一篇文章:http://www.cnblogs.com/pingge/articles/3153571.html ...
- [百度空间] [原]DLL导出实例化的模板类
因为模板是在编译的时候根据模板参数实例化的,实例化之后就像一个普通的类(函数),这样才有对应的二进制代码;否则,没有模板参数,那么编译器就不知道怎么生成代码,所以生成的DLL就没有办法导出模板了.但是 ...
- Map中如何把没有定义操作符<的类作为key
Map中如何把没有定义操作符<的类作为key 其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序 ...
随机推荐
- web性能优化指南
前端性能优化,是每个前端必备的技能,优化自己的代码,使自己的网址可以更加快速的访问打开,减少用户等待,今天就会从几个方面说起前端性能优化的方案, 看下面的一张图,经常会被面试官问,从输入URL到页面加 ...
- zabbix主动模式无法获取网卡和文件系统数据
zabbix版本为4.2,根据网上教程将zabbixagent设置成主动模式后,将templates中各Items的type改为Zabbix agent (active),同时将Discovery r ...
- 管程(Moniter): 并发编程的基本心法
JavaStorm 关注公众号获取更多并发 在吃透 Syncchronized 原理 中介绍了关于 Synchronize的实现原理,无论是同步方法还是同步代码块,无论是ACC_SYNCHRONIZE ...
- Android Studio 安装教程
前言 前段时间周围有很多认识的人学习Android,看蛮多人在装Android Studio,然而看他们装的过程不是那么顺利?然后也有高中同学来问我,于是乎就自己也试着去装了下,也方便日后学习Andr ...
- 爬虫(九):python操作MySQL、MongoDB
1. python操作MySQL 1.1 MySQL基础 在java基础部分就写过了. https://www.cnblogs.com/liuhui0308/p/11891844.html 1.2 p ...
- 松软科技Web课堂:JavaScript Break 和 Continue
break 语句“跳出”循环. continue 语句“跳过”循环中的一个迭代. Break 语句 在本教程稍早的章节中,您已见到了 break 语句.它被用于“跳出” switch 语句. brea ...
- leaflet视频监控播放(附源码下载)
前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...
- 内网渗透教程大纲v1.0
内网渗透 ☉MS14-068(CVE-2014-6324)域控提权利用及原理解析 ☉域控权限提升PTH攻击 未完待续...
- oracle 字符串转为数字排序
select * from user order by to_number(dept_id) asc
- 让终端更好看--Ubuntu OhMyZsh配置指南
查看shell列表 cat /etc/shells 如果发现没有zsh就安装 安装zsh sudo apt install zsh 设置默认shell chsh -s $(which zsh) 重启主 ...