C++ 模板套模板

1.模板类中有成员是模板

#include <iostream>
#include <typeinfo>
using  namespace  std;
 
template< class  T>
class  Outer
{
public :
   template< class  R>
   class  Inner
   {
   public :
     void  f();
   };
};
 
template< class  T>
template < class  R>
void  Outer<T>::Inner<R>::f()
{
   cout << "Outer == "  << typeid(T).name() << endl;
   cout << "Inner == "  << typeid(R).name() << endl;
   cout << "Full Inner == "  << typeid(* this ).name() << endl;
}
 
int  main()
{
   Outer< int >::Inner< bool > inner;
   inner.f();
} /**/ ///:~
 
//output:
//Outer == int
//Inner == bool
//Full Inner == class Outer<int>::Inner<bool>
//Press any key to continue . . .

  2、模板类参数是模板

// A print function for standard C++ sequences
#include <iostream>
#include <list>
#include <memory>
#include <vector>
#include <deque>
using  namespace  std;
 
template< class  T, template< class  U, class  = allocator<U> >
                   class  Seq>
void  printSeq(Seq<T>& seq)
{
   for  (typename Seq<T>::iterator b = seq.begin();
        b != seq.end();)
     cout << *b++ << endl;
}
 
int  main()
{
   // Process a vector
   vector< int > v;
   v.push_back(1);
   v.push_back(2);
   printSeq(v);
   // Process a list
   list< int > lst;
   lst.push_back(3);
   lst.push_back(4);
   printSeq(lst);
 
   // Process a deque
   deque< int > d;
   d.push_back(5);
   d.push_back(6);
   printSeq(d);
} /**/ ///:~

  注意:typename 通知编译器被限定的标识符应该为类型,不同与typedef 是定义新的类型。

原文地址


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/11/21/2257148.html,如需转载请自行联系原作者

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值