首页 > 其他 > 详细

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

时间:2020-03-20 14:43:18      阅读:41      评论:0      收藏:0      [点我收藏+]

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);
}

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!