博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C++】模板参数推导(template argument deduction)
阅读量:7102 次
发布时间:2019-06-28

本文共 2983 字,大约阅读时间需要 9 分钟。

<?xml version="1.0" encoding="gb18030"?>

 

 

Table of Contents

1 模板参数推导在迭代器中的使用

在算法中运用迭代器时,可能会用到其相应类型(associative type),即迭代器所指向对象的类别。但C++只支持sizeof(),并不存在typeof()之说(即使运用RTTI性质中的typeid()获得的也只是类型名称不能用来做变量声明之用)。

为解决此问题,可以利用函数模板(function template)的参数推导(argument deduction)机制:

 

template 
void fun_impl(I iter, T t){ // 此处该函数利用模板参数推导得知T为*iter类型 T tmp; // 可以声明变量 //...};template
inline void fun(I iter){ fun_impl(iter, *iter); //此处把*iter作为第二个参数传递给fun_impl()}int main(int argc, char *argv[]){ int i; fun(&i); return 0;}

再例如:

 

template
void insertSort(const I& begin, const I& end, Cmp lessThan); template
void _insertSort(const I& begin, const I& end, const T t); template
void _insertSort(const I& begin, const I& end, Cmp lessThan, const T& t); template
void insertSort(const I& begin, const I& end){ if(begin != end) _insertSort(begin, end, *begin); // 把*begin传递做为第三个参数} template
// 此时__insertSort利用模板参数推导机制推测出T的类型,其实就是上个函数insertSort(const I& begin, const I& end)中迭代器begin所指向对象的类型void _insertSort(const I& begin, const I& end, const T t){ insertSort(begin, end, less
()); //此时T已被推测出来可以直接使用} template
void insertSort(const I& begin, const I& end, Cmp lessThan){ if(begin != end) _insertSort(begin, end, lessThan, *begin);} //算法的具体实现 template
void _insertSort(const I& begin, const I& end, Cmp lessThan, const T& t){ I j; for(I i = begin+1; i != end; ++i){ T tmp = *i; //通过参数推导确定T类型 for(j = i; j != begin && lessThan(tmp, *(j-1)); --j) *j = *(j-1); *j = tmp; } }

参数为两个迭代器的函数insertSort把其中一个迭代器解引用后传递给参数为两个迭代器和一个迭代器指向对象类型的三个参数的__insertSort函数,用户并不关心底层如何实现,只需要传递两个参数即可,并未告知函数迭代器指向对象的类型但可以通过模板参数推导机制来推测,从而方便用户。

2 模板参数引用与非引用的区别

当模板参数是非引用时会导致模板参数推断衰减(decaying)(把数组和函数类型变成指针类型、去掉const, volatile等修饰符)。

例如:

 

template
void f(T); //PisT template
void g(T&); // P is also T double x[20]; int const seven = 7; f(x); // nonreference parameter: T is double* g(x); // reference parameter: T is double[20] f(seven); // nonreference parameter: T is int g(seven); // reference parameter: T is int const f(7); // nonreference parameter: T is int g(7); // reference parameter: T is int => ERROR: can't pass 7 to int&

f是非引用模板参数,所以会decaying 把double[]变成double*,同理int const => int,最后一个传递是非法的,因为不能把常数作为int&。

再例如:

 

#include 
#include
using namespace std;template
inline const T& Max(const T& a, const T& b){ return a
inline T MMax(T a, T b){ return a
void ref(const T& t){ cout<<"Ref: "<
<
void nonref(T t){ cout<<"Nonref: "<
<

Author: visayafan

Date: 2011-11-29 21:02:01

HTML generated by org-mode 6.33x in emacs 23

转载地址:http://djchl.baihongyu.com/

你可能感兴趣的文章
如何在Windows Server 2008 R2下搭建FTP服务
查看>>
人才市场的IT职位分析
查看>>
ETL,你的系统可以吗?
查看>>
如何选择正确的产品路线图
查看>>
VIM7.3中文手册
查看>>
Python文件夹与文件的操作
查看>>
Deep learning:四十八(Contractive AutoEncoder简单理解)
查看>>
NSDate和NSString相互转换 (转)
查看>>
ASCII Unicode UTF-8 之间的关系
查看>>
计算四则运算表达式(Java语言实现)
查看>>
5、反射-动态代理
查看>>
C++中的类型转换
查看>>
Linux内核的冷热缓存
查看>>
Rop攻击
查看>>
USACO习题:Palindromic Squares
查看>>
C语言中有bool类型吗?
查看>>
保护你的手腕!手腕健康度小检测
查看>>
LVM更换硬盘
查看>>
Helvetic Coding Contest 2017 online mirror M&N&O. April Fools' Problem
查看>>
Openjudge1388 Lake Counting【DFS/Flood Fill】
查看>>