template
来自cppreference.com
[编辑] 声明泛型对象
语法:
template <class data-type> return-type name( parameter-list ) { statement-list; }
模板用于创建泛型函数和泛型类,可以在不知道数据类型的情况下操作数据。这是通过占位符 data-type 实现的,其他 数据类型可以取代占位符。
例如,下面的代码使用了模板来定义一个泛型交换函数,它可以交换任一类型的两个变量:
template<class X> void genericSwap( X &a, X &b ) { X tmp; tmp = a; a = b; b = tmp; } int main(void) { ... int num1 = 5; int num2 = 21; cout << "Before, num1 is " << num1 << " and num2 is " << num2 << endl; genericSwap( num1, num2 ); cout << "After, num1 is " << num1 << " and num2 is " << num2 << endl; char c1 = 'a'; char c2 = 'z'; cout << "Before, c1 is " << c1 << " and c2 is " << c2 << endl; genericSwap( c1, c2 ); cout << "After, c1 is " << c1 << " and c2 is " << c2 << endl; ... return( 0 ); }
下面的模板定义了一个泛型类:
#include <cassert> const unsigned int maxSize = 20; template<class T> class simpleStack { public: simpleStack(): amount(0) {} bool empty() const { return amount == 0; } bool full() const { return amount == maxSize; } unsigned int size() const { return amount; } void clear() { amount = 0; } const T& top() const; void pop(); void push( const T &x); private: unsigned int amount; T array[ maxSize ]; }; template<class T> const T& simpleStack<T>::top() const { assert( !empty() ); return array[ amount - 1 ]; } template<typename T> /* 这里把 class 换成了 typename,这两者是等价的 */ void simpleStack<T>::pop() { assert( !empty() ); --amount; } template<typename T> void simpleStack<T>::push(const T &x) { assert( !full() ); array[ amount++ ] = x; } #include <iostream> /* main code */ int main() { simpleStack< int > aIntStack; int i = 100; while ( !aIntStack.full() ) aIntStack.push( i++ ); cout << "stack size: " << aIntStack.size() << endl; return 0; }
[编辑] Disambiguating Dependent-Names
The template keyword is also used to indicate that a dependent name refers to a template. In contrast to the placement of the typename keyword, the template keyword is placed just before the name of the template member. For example:
struct Foo { template<typename T> struct Member { typedef int type; }; }; template<class T> struct Bar { typedef typename T::template Member<int>::type bar; }; typedef Bar<Foo> FooBar;
相关主题: typename