显式实例化解决模板导出静态动态库
前言
模板生成动态库与测试
#pragma once
#include <iostream>
#include <string>
template<typename T>
void f(T);
template<typename T>
struct __declspec(dllexport) X {
void f();
};模板生成静态库与测试
总结
Last updated
#pragma once
#include <iostream>
#include <string>
template<typename T>
void f(T);
template<typename T>
struct __declspec(dllexport) X {
void f();
};Last updated
#include "export_template.h"
template<typename T>
void f(T) { // 函数模板定义
std::cout << typeid(T).name() << '\n';
}
template <typename T>
void X<T>::f(){ // 类模板中的成员函数模板定义
std::cout << typeid(T).name() << '\n';
}
template __declspec(dllexport) void f<int>(int);
template __declspec(dllexport) void f<std::string>(std::string);
template struct X<int>; // 类模板显式实例化#include "export_template.h"
#include <string>
int main(){
std::string s;
f(1);
//f(1.2); // Error!链接错误,没有这个符号
f(s);
X<int>x;
x.f();
}