模板笔记8 包含模型和显式实例化

print.hpp

#ifndef __LINK_HPP
#define __LINK_HPP
#include <iostream>
#include <typeinfo>
template<typename T>
void print(T const&);
#include "print.cpp"
template void print<double>(double const &);//显式实例化,double只能显式实例化一次。实例化可以在需要的使用才进行,避免包含庞大头文件的开销
#endif

-------------------------------------------------

print.cpp

template<typename T>
void print(T const& x)
{
std::cout << typeid(x).name() << std::endl;
}

-------------------------------------------------

main.cpp

#include "print.hpp"

int main()
{
double b = 3.1415926;
print(b);
}

------------------------------------------------

原文地址:https://www.cnblogs.com/xpylovely/p/12531485.html