c++ template(5)模板实战
一.包含模型
一份头文件hpp,一份cpp实现文件
hpp:
#ifndef MYFIRST_HPP #define MYFIRST_HPP// declaration of template template <typename T> void print_typeof (T const&);#endif // MYFIRST_HPP
cpp:
#include <iostream> #include <typeinfo> #include "myfirst.hpp"// implementation/definition of template template <typename T> void print_typeof (T const& x) {std::cout << typeid(x).name() << std::endl; }
使用模板函数:
#include "myfirst.hpp" // use of the template int main() {double ice = 3.0;print_typeof(ice); // call function template for type double }
将会导致链接错误,必须有一个基于double实例化的函数定义
为了通过编译,有2两种办法:
1.把cpp文件包含在头文件里面
2.在使用模板的文件中引用cpp文件
3.不要cpp文件,将声明和实现都放在hpp文件里面
#define MYFIRST_HPP#include <iostream> #include <typeinfo>// declaration of template template <typename T> void print_typeof (T const&);// implementation/definition of template template <typename T> void print_typeof (T const& x) {std::cout << typeid(x).name() << std::endl; }#endif // MYFIRST_HPP
开销:
1.增加了头文件的大小
2.增加了编译复杂度
二.手工显式实例化
声明一个显式实例化的头文件
#include "myfirst.hpp"// explicitly instantiate print_typeof() for type double template void print_typeof<double>(double const&);
现在使用这个头文件,编译正常
#include "myfirstinst.cpp" // use of the template int main() {double ice = 3.0;print_typeof(ice); // call function template for type double }
优点:避免了庞大的头文件开销
注意点:一个程序中最多只有一个显式实例化体
三.整合包含模型和显式实例化
如上声明3份文件.
- 1份头文件
- 1份实现文件
- 一份显式实例化文件
如下2种用法:
第一种使用应该引用“stackdef.hpp”
c++ template(5)模板实战相关推荐
- C++_member template成员模板
C++_member template成员模板
- Django Template 网页模板(五)
Django Template 网页模板 6. Template 网页模板 6.1 Templeate 基础知识 6.2 Templeate 小案例 6.3 Template 标签 6. Templa ...
- C++模板实战4:模板特化
模板设计的目的就是为了通用,但是某些情形下也有特殊情形独立于通用规则,因此模板需要针对特殊情形进行特化. 1 类模板的特化,在模板类名自后<>内写上特化的类型名,其中<>这部 ...
- template.js模板引擎下载和实例
定义: art-template 是一个简约.超快的模板引擎.它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支持 NodeJS 和浏览器 下面 ...
- template(标签模板)
template(标签模板) 作用:将组件的模板内容通过标签模板的形式展示出来,其目的为了书写简便. <!DOCTYPE html> <html lang="en" ...
- Template类模板
Template 1.函数模板 2.类模板 3.成员模板 4.嵌套类模板 5.总结 1.函数模板 编译期间编译器自动分析参数类型,template函数模板是支持默认参数的,T1 .T2 顺序在默认情况 ...
- template标签_Django实战: 利用自定义模板标签实现仿CSDN博客月度归档
应网友慕之岩的请求,现提供下Django项目中如何使用自定义标签实现仿CSDN博客的月度归档(如下图所示).要求按月统计每个月发表的博文篇数, 跳过空白月份,最后结果按发布时间逆序排列.点击每个月份可 ...
- C++ template类模板实现栈 pop push
心得 data = new T[size];这句太坑了,一开始写成了data = new T(size)圆括号,所以一直随机性报错. 我本来以为是ide的问题,在vs2017里就会崩,在dev里不会崩 ...
- C++ template函数模板
定义一个函数模板,任何类型变量均可使用 #include<iostream> #include<string>template<class T> //或者使用tem ...
最新文章
- R语言ggplot2可视化使用ggplot2包patchwork包在可视化结果(右上角)中插入logo图片
- 英特尔是个可以讲理的地儿
- React Native 0.59.0 发布,使用 React 编写原生应用
- Oracle常用数据库系统表单以及SQL的整理
- 配置mysql环境变量
- java后端传object给js_【JSON】JSON在前端和后端传递
- 如何给影像添加投影_地面互动投影是如何实现的,需要哪些设备?
- UIKit框架之NSObject
- chapter8.1、面向对象
- python gevent async_详解python之协程gevent模块
- 网外(Internet)访问代理服务器内部的实现方法
- [C++]2-3 倒三角形
- 环信移动客服客服昵称头像设置
- 回路、简单回路、简单路径
- ValueError: mode mismatch
- Gartner 魔力象限:安全信息和事件管理 2020
- 你必须知道的家庭急救常识
- Instant Contiki 安装笔记——SDCC和CC2530
- Vue 开发必须知道的 36 个技巧【近1W字】
- Java设计模式:(一)动态代理分析 (含静态代理)