=====template=====
=== Declaring Generic Objects ===
Syntax:
template return-type name( parameter-list ) {
statement-list;
}
Templates are used to create generic functions and generic classes and can operate on data without
knowing the nature of that data. They accomplish this by using a placeholder
data-type for which many other [[/data_types|data types]] can be substituted.
For example, the following code uses a template to define a generic swap
function that can swap two variables of any type:
template 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 );
}
The next template is used to descibe a generic class:
#include
const unsigned int maxSize = 20;
template
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
const T& simpleStack::top() const
{
assert( !empty() );
return array[ amount - 1 ];
}
template /*it's allowed and equal to replace class with typename*/
void simpleStack::pop()
{
assert( !empty() );
--amount;
}
template
void simpleStack::push(const T &x)
{
assert( !full() );
array[ amount++ ] = x;
}
#include
/* 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|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
struct Member {
typedef int type;
};
};
template
struct Bar {
typedef typename T::template Member::type bar;
};
typedef Bar FooBar;
Related Topics: [[typename]]