模板高级用法: Template Template Argument

注意,template template parameter 是极晚近才加入的C++ 特性,因此上面这个程序可作为一个极佳工具,用来评估你的编译器对 template 特性的支持程度。

下面的代码在vs2008通过, codeblocks 通过, vs2003不通过

  1. // stack8.cpp : Defines the entry point for the console application.
  2. //

  3. #include "stdafx.h"
  4. #include <deque>
  5. #include <vector>
  6. #include <iostream>
  7. #include <memory>
  8. #include <string>
  9. #include <exception>

  10. using namespace std;

  11. template <typename T, 
  12. template <typename ELEM, typename = allocator<ELEM> >
  13. class CONT = deque >

  14. class CStack
  15. {
  16. public:
  17.     void push(T const&);
  18.     void pop();
  19.     T top() const;
  20.     bool empty() const;

  21.     // 赋予一个「元素类型为 T2」的 stack
  22.     template <class T2,
  23.         template <class ELEM2, class = allocator<ELEM2> >
  24.     class CONT2>
  25.         CStack<T, CONT>& operator = (CStack<T2, CONT2> const&);

  26. private:
  27.     CONT<T> m_dqElems;
  28. };

  29. template <class T, template<classclassclass CONT>
  30. bool CStack<T, CONT>::empty() const
  31. {
  32.     return m_dqElems.empty();
  33. }

  34. template <class T, template <classclassclass CONT>
  35. void CStack<T, CONT>::push(T const& elem)
  36. {
  37.     m_dqElems.push_back(elem);
  38. }

  39. template <class T, template<classclassclass CONT>
  40. void CStack<T, CONT>::pop()
  41. {
  42.     if (m_dqElems.empty())
  43.     {
  44.         throw out_of_range("stack empty!");
  45.     }

  46.     m_dqElems.pop_back();
  47. }

  48. template <class T, template<classclassclass CONT>
  49. T CStack<T, CONT>::top() const
  50. {
  51.     if (m_dqElems.empty())
  52.     {
  53.         throw out_of_range("stack empty!");
  54.     }

  55.     return m_dqElems.back(); //传回最后一个元素的拷贝
  56. }

  57. template <class T, template<classclassclass CONT>
  58. template <class T2, template<classclassclass CONT2>
  59. CStack<T, CONT>& CStack<T, CONT>::operator = (CStack<T2, CONT2> const& op2)
  60. {
  61.     if ((void*)this == (void*)&op2)// 是否赋值给自己
  62.     {
  63.         return *this;
  64.     }

  65.     CStack<T2, CONT2> tmpOp2(op2);
  66.     m_dqElems.clear();

  67.     while (!tmpOp2.empty())
  68.     {
  69.         m_dqElems.push_front(tmpOp2.top());
  70.         tmpOp2.pop();
  71.     }

  72.     return *this;
  73. }

  74. template <class T, template<classclassclass CONT>
  75. void PrintStack(CStack<T, CONT> const& objStack)
  76. {
  77.     //做一个copy打印
  78.     CStack<T, CONT> tmpStack(objStack);

  79.     while (!tmpStack.empty())
  80.     {
  81.         cout << tmpStack.top() << "/t";
  82.         tmpStack.pop();
  83.     }

  84.     cout << endl;
  85. }

  86. int main()
  87. {
  88.     int i = 0;
  89.     try
  90.     {
  91.         CStack<int> intStack;
  92.         CStack<float> floatStack;

  93.         for (i = 0; i < 10; i++)
  94.         {
  95.             intStack.push(11 * i);
  96.         }

  97.         cout << "intStack contents: " << endl;
  98.         PrintStack<int>(intStack);

  99.         floatStack = intStack;
  100.         cout << "floatStack contents: " << endl;
  101.         PrintStack<float>(floatStack);
  102.     }
  103.     catch (exception const& exp)
  104.     {
  105.         cerr << "Exception: " << exp.what() << endl;
  106.     }

  107.     CStack<int, vector> vtStack;

  108.     for (i = 0; i < 10; i++)
  109.     {
  110.         vtStack.push(2 * i);
  111.     }

  112.     PrintStack<int, vector>(vtStack);

  113.     return 0;
  114. }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值