首页 > 其他 > 详细

Traits技法

时间:2017-11-01 00:58:27      阅读:301      评论:0      收藏:0      [点我收藏+]

扮演“特性萃取机”角色,萃取各个迭代器的特性(迭代器的相应类型)

通过class template partial specialization的作用,不论是原生指针或class-type iterators,都可以让外界方便地取其相应类别

  

#pragma once

template <class T>
class MyIter
{
public:
	MyIter(T *p = 0):ptr(p){}
	MyIter(MyIter<T> &ths):ptr(ths.ptr){}
	T& operator*(){return *ptr;}

	typedef T value_type;

private:
	T *ptr;	
};


// Partial Specialization 偏特化
template <class T>
struct iterator_traitse
{
	typedef typename T::value_type value_type;
};

template <class T>
struct iterator_traitse<T *>
{
	typedef T value_type;
};

template <class T>
typename iterator_traitse<T *> :: value_type fun(T ite)
{
	return *ite;
}

int main()
{
	MyIter<int> ite(new int(8));
	
	// cannot convert from ‘MyIter<T>‘ to ‘int‘
	int n = fun(ite);

	return 0;
}

 

Traits技法

原文:http://www.cnblogs.com/xiaobingqianrui/p/7764532.html

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