本文首发于个人博客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

History

  • 20191012: created.

Copyright

如何在动态链接库dll/so中导出自定义的模板类template class | how to implement a template class with c++ and export in dll/so的更多相关文章

  1. ArcGIS api fo silverlight学习三(利用ElementLayer实现鼠标悬浮弹出自定义窗体)

    接着上一节继续学习,本节主要是利用ElementLayer实现鼠标悬浮弹出自定义窗体 参考博文:http://www.cnblogs.com/luxiaoxun/p/3322218.html 一.新建 ...

  2. 实现android手机来电拦截系统页面弹出自定义页面特效

    如何实现android手机来电拦截系统页面弹出自定义页面特效, 首先:    我们需要注册一个监听来电的广播PhoneStateReceiver 类:其次:    在onReceive里面我们获取an ...

  3. android高德地图网络路径实现自定义marker并点击弹出自定义窗口

    android中使用地图的地方随处可见,今天记录一下网络路径生成自定义marker,点击标记弹出自定义的窗口(在这里使用的是高德地图) 在这里我们使用Grilde去加载网络图片,因为这个简直太方便了! ...

  4. 【WPF】右下角弹出自定义通知样式(Notification)——简单教程

    原文:[WPF]右下角弹出自定义通知样式(Notification)--简单教程 1.先看效果 2.实现 1.主界面是MainWindow 上面就只摆放一个Button即可.在Button的点击事件中 ...

  5. MFC DLL 导出函数的定义方式

    一直在鼓捣DLL,每天的工作都是调试一个一个的DLL,往DLL里面添加自己的代码,但是对于DLL一直不太了解啊!今天一查资料,才发现自己对于DLL编写的一些基本知识也不了解.要学习,这篇文章先总结DL ...

  6. DLL中导出STL模板类的问题

    接上一篇. 上一篇的dll在编译过程中一直有一个警告warning C4251: ‘CLASS_TEST::m_structs’ : class ‘std::vector<_Ty>’ ne ...

  7. DLL模块例2:使用__declspec(dllexport)导出函数,extern "C"规范修饰名称,隐式连接调用dll中函数

    以下内容,我看了多篇文章,整合在一起,写的一个例子,关于dll工程的创建,请参考博客里另一篇文章:http://www.cnblogs.com/pingge/articles/3153571.html ...

  8. [百度空间] [原]DLL导出实例化的模板类

    因为模板是在编译的时候根据模板参数实例化的,实例化之后就像一个普通的类(函数),这样才有对应的二进制代码;否则,没有模板参数,那么编译器就不知道怎么生成代码,所以生成的DLL就没有办法导出模板了.但是 ...

  9. Map中如何把没有定义操作符<的类作为key

    Map中如何把没有定义操作符<的类作为key 其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序 ...

随机推荐

  1. python学习-文件创建读取

    # 文件创建 # 读写# 文件存在?不存在?在操作系统上# 读 read r 写 write w# 打开一个文件# fs = open("xiaojian.txt",encodin ...

  2. Ubuntu18.04 配置Cups PDF虚拟打印机服务

    更新 sudo apt update && sudo apt upgrade -y 安装cups pdf服务 sudo apt-get install cups-pdf -y 修改配置 ...

  3. RabbitMQ之交换机及spring整合

    交换机 交换机属性: Name:交换机名称 Type:交换机类型 direct.topic.fanout.headers Durability:是否需要持久化,true为持久化 Auto Delete ...

  4. CentOS7.2下部署zabbix4.0

    整体部署采用centos7+php+apache+mariadb 基础环境配置优化 1. 关闭防火墙 [root@monitor_53 ~]$ systemctl stop firewalld [ro ...

  5. Typroa 常用快捷键

    Typora 常用快捷键 文件操作 Ctrl + N :新建文件 Ctrl + shift + N :新建窗口 Ctrl + O :打开 Ctrl + P : 快速打开(快速打开之前编辑过的历史文件) ...

  6. HttpRunner学习10--hook机制

    前言 对于使用过 Python结合Unittest 框架来做自动化测试的同学,应该知道在 Unittest 中,有这样2个方法:setUp() 和 tearDown() ,即前置和后置操作.通常 se ...

  7. C#总结(七)动态加载C++动态链接库

    C#调用C++ 链接库的方式分为静态调用和动态调用这两种方式.静态调用之前的文章里面都有介绍,使用.net 提供的DllImport 导入相关的C++ 库即可.请看之前的文章,https://www. ...

  8. 一起学Vue之样式绑定

    在前端开发中,设置元素的 class 列表和内联样式是基本要求.本文主要讲解Vue开发中,样式列表和内联样式的绑定,仅供学习分享使用,如果有不足之处,还请指正. 概述 Vue操作元素的 class 列 ...

  9. abp示例项目BookStore搭建部署

    之前部署过BookStore项目,但是换了新电脑也想好好学习下这个示例项目,于是在新电脑上重新拉了Git上的ABP项目代码,一编译生成BookStore项目就报错,可以参考 abp示例项目BookSt ...

  10. 无法Google的解决方案

    献给新入开发行业的小伙伴. 本文不会事无巨细的讲解每一个细节,只是为读者提供一个路线图,并提供相应的参考资料. 为了更高效的解决各种技术问题,有时不得不到墙外去寻找解决方案.每个开发者效率高了,宏观来 ...