模板高级用法: Template Template Argument
2008-12-02 15:12
405 查看
注意,template template parameter 是极晚近才加入的C++ 特性,因此上面这个程序可作为一个极佳工具,用来评估你的编译器对 template 特性的支持程度。
下面的代码在vs2008通过, codeblocks 通过, vs2003不通过
// stack8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <deque>
#include <vector>
#include <iostream>
#include <memory>
#include <string>
#include <exception>
using namespace std;
template <typename T,
template <typename ELEM, typename = allocator<ELEM> >
class CONT = deque >
class CStack
{
public:
void push(T const&);
void pop();
T top() const;
bool empty() const;
// 赋予一个「元素类型为 T2」的 stack
template <class T2,
template <class ELEM2, class = allocator<ELEM2> >
class CONT2>
CStack<T, CONT>& operator = (CStack<T2, CONT2> const&);
private:
CONT<T> m_dqElems;
};
template <class T, template<class, class> class CONT>
bool CStack<T, CONT>::empty() const
{
return m_dqElems.empty();
}
template <class T, template <class, class> class CONT>
void CStack<T, CONT>::push(T const& elem)
{
m_dqElems.push_back(elem);
}
template <class T, template<class, class> class CONT>
void CStack<T, CONT>::pop()
{
if (m_dqElems.empty())
{
throw out_of_range("stack empty!");
}
m_dqElems.pop_back();
}
template <class T, template<class, class> class CONT>
T CStack<T, CONT>::top() const
{
if (m_dqElems.empty())
{
throw out_of_range("stack empty!");
}
return m_dqElems.back(); //传回最后一个元素的拷贝
}
template <class T, template<class, class> class CONT>
template <class T2, template<class, class> class CONT2>
CStack<T, CONT>& CStack<T, CONT>::operator = (CStack<T2, CONT2> const& op2)
{
if ((void*)this == (void*)&op2)// 是否赋值给自己
{
return *this;
}
CStack<T2, CONT2> tmpOp2(op2);
m_dqElems.clear();
while (!tmpOp2.empty())
{
m_dqElems.push_front(tmpOp2.top());
tmpOp2.pop();
}
return *this;
}
template <class T, template<class, class> class CONT>
void PrintStack(CStack<T, CONT> const& objStack)
{
//做一个copy打印
CStack<T, CONT> tmpStack(objStack);
while (!tmpStack.empty())
{
cout << tmpStack.top() << "/t";
tmpStack.pop();
}
cout << endl;
}
int main()
{
int i = 0;
try
{
CStack<int> intStack;
CStack<float> floatStack;
for (i = 0; i < 10; i++)
{
intStack.push(11 * i);
}
cout << "intStack contents: " << endl;
PrintStack<int>(intStack);
floatStack = intStack;
cout << "floatStack contents: " << endl;
PrintStack<float>(floatStack);
}
catch (exception const& exp)
{
cerr << "Exception: " << exp.what() << endl;
}
CStack<int, vector> vtStack;
for (i = 0; i < 10; i++)
{
vtStack.push(2 * i);
}
PrintStack<int, vector>(vtStack);
return 0;
}
下面的代码在vs2008通过, codeblocks 通过, vs2003不通过
// stack8.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <deque>
#include <vector>
#include <iostream>
#include <memory>
#include <string>
#include <exception>
using namespace std;
template <typename T,
template <typename ELEM, typename = allocator<ELEM> >
class CONT = deque >
class CStack
{
public:
void push(T const&);
void pop();
T top() const;
bool empty() const;
// 赋予一个「元素类型为 T2」的 stack
template <class T2,
template <class ELEM2, class = allocator<ELEM2> >
class CONT2>
CStack<T, CONT>& operator = (CStack<T2, CONT2> const&);
private:
CONT<T> m_dqElems;
};
template <class T, template<class, class> class CONT>
bool CStack<T, CONT>::empty() const
{
return m_dqElems.empty();
}
template <class T, template <class, class> class CONT>
void CStack<T, CONT>::push(T const& elem)
{
m_dqElems.push_back(elem);
}
template <class T, template<class, class> class CONT>
void CStack<T, CONT>::pop()
{
if (m_dqElems.empty())
{
throw out_of_range("stack empty!");
}
m_dqElems.pop_back();
}
template <class T, template<class, class> class CONT>
T CStack<T, CONT>::top() const
{
if (m_dqElems.empty())
{
throw out_of_range("stack empty!");
}
return m_dqElems.back(); //传回最后一个元素的拷贝
}
template <class T, template<class, class> class CONT>
template <class T2, template<class, class> class CONT2>
CStack<T, CONT>& CStack<T, CONT>::operator = (CStack<T2, CONT2> const& op2)
{
if ((void*)this == (void*)&op2)// 是否赋值给自己
{
return *this;
}
CStack<T2, CONT2> tmpOp2(op2);
m_dqElems.clear();
while (!tmpOp2.empty())
{
m_dqElems.push_front(tmpOp2.top());
tmpOp2.pop();
}
return *this;
}
template <class T, template<class, class> class CONT>
void PrintStack(CStack<T, CONT> const& objStack)
{
//做一个copy打印
CStack<T, CONT> tmpStack(objStack);
while (!tmpStack.empty())
{
cout << tmpStack.top() << "/t";
tmpStack.pop();
}
cout << endl;
}
int main()
{
int i = 0;
try
{
CStack<int> intStack;
CStack<float> floatStack;
for (i = 0; i < 10; i++)
{
intStack.push(11 * i);
}
cout << "intStack contents: " << endl;
PrintStack<int>(intStack);
floatStack = intStack;
cout << "floatStack contents: " << endl;
PrintStack<float>(floatStack);
}
catch (exception const& exp)
{
cerr << "Exception: " << exp.what() << endl;
}
CStack<int, vector> vtStack;
for (i = 0; i < 10; i++)
{
vtStack.push(2 * i);
}
PrintStack<int, vector>(vtStack);
return 0;
}
相关文章推荐
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- C++模板template用法总结
- Yii2 – 安装高级模板 advanced project template
- C++惯用法:通过成员模板实现隐式转换(Coercion by Member Template)
- template-web.js模板引擎的使用之高级篇
- C++ template的一些高级用法(元编码,可变参数,仿函数,using使用方法,. C++ 智能指针)
- C++模板template用法总结
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- C++模板template用法
- C++模板的高级用法中Typelist的用法
- C++模板template用法总结
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- StringTemplate.net模板技术用法
- c++模板中的 typename 和 template 关键字用法
- EMACS Verilog-mode 高级用法 AUTO_TEMPLATE
- thinkjs——art-template模板用法
- C++模板template用法总结
- VTemplate模板引擎的使用--高级篇
- 【C++】template template argument 模板参数模板
- C++模板template用法总结