如何在动态链接库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的大小顺序 ...
随机推荐
- 3个Spring Boot核心注解,你知道几个?
Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...
- python学习-logging
"""#设置输出的日志内容格式fmt = '%(asctime)s %(filename)s %(funcName)s [line:%(lineno)d] %(level ...
- 简单学习【1】——使用webpack
使用webpack webpack命令 webpack配置 第三方脚手架 1.webpack命令 webpack - h (webpack 所有的选项) webpack -v (查看webpack的版 ...
- django基础之day08,分页器从无到有,动态思路解析全过程
*********分页器从无到有的全过程,动态思路解析如下:******** 1.通过book_queryset = models.Book.objects.all()[start_num:end_n ...
- 《Java Spring框架》SpringXML配置详解
Spring框架作为Bean的管理容器,其最经典最基础的Bean配置方式就是纯XML配置,这样做使得结构清晰明了,适合大型项目使用.Spring的XML配置虽然很繁琐,而且存在简洁的注解方式,但读懂X ...
- Linux中的 date 使用
01. 日期格式字符串列表 %H 小时(以00-23来表示). %I 小时(以01-12来表示). %K 小时(以0-23来表示). %l 小时(以0-12来表示). %M 分钟(以00-59来表示) ...
- Spring基础——IOC九种bean声明方式
Spring简介 Spring不是服务于开发web项目的功能,或业务.而是服务于项目的开发,方便各层间的解耦调用,方便对类的批量管理,是提高软件开发效率,降低后期维护成本的框架. Spring的核心思 ...
- Gradle for Android ( 构建变体 )
链接: http://77blogs.com/?p=38 https://www.cnblogs.com/tangZH/p/10999060.html 有时候我们一个app需要有不同的版本,不同的版本 ...
- [Windows] 智慧职教刷课软件(职教雨滴1.9更新完成)
(智慧职教刷课软件-职教雨滴)支持职教云(云课堂)的课程 2019年10月17日 16:19:57 增加支持资料库,MOOC 点击链接加入群聊[职教雨滴反馈群]:https://jq.qq.com/? ...
- Java题库——Chapter15 事件驱动编程和动画
Chapter 15 Event-Driven Programming and Animations Section 15.2 Events and Event Sources1. A Java ...