Вы находитесь на странице: 1из 15

Templates

Templates are blueprints from which classes


and/or functions automatically generated by the
compiler based on a set of parameters.

Each time a template is used with different


parameters is used, a new version of the class or
function is generated.

A new version of a class or function is called


specialization of the template.
104

Class Templates

Class templates introduce class blueprints.

Class templates start with the keyword template


followed by a list of type or non-type parameters
enclosed in angle brackets.

Type parameters are preceded by the keyword


class or typename.

105

Class Template Array


template <class Type>
class Array
{
private:
Type* fBufferForElements;
static int fNumberOfArrays;
public:

bool insert( const Type& aElement, int aPos );

};
106

Class Template Instantiation


Array<int> counters;
Array<Account> accounts;

Types uses as arguments cannot be classes


with local scope.

Once instantiated, a class template can be


used as any other class.
107

Template Member Functions

Member functions defined outside of the class


template must be parameterized as well:
template <class Type>
bool Array<Type>::insert( const Type& aElement, int aPos )
{

};

108

Class Template Constructor

template <class Type>


Array<Type>::Array()
{

};

109

Static Template Member

template <class Type>


int Array<Type>::fNumberOfArrays = 0;

110

Template Functions

Plain functions can be parameterized as well:


template <class Type>
void xchg( Type& x, Type& y )
{
Type t = x;
x = y;
y = t;
}
111

Arguments to Template Functions

Template functions often take class templates as


arguments, as do member functions of class
templates:
template <class Type>
bool insert( Array<Type>& a, Type& elem, int pos )
{
return a.insert( elem, pos );
}

112

Explicit Specialization

An explicit specialization of a template defines a specific


form of the template to use for a certain set of parameters:

template <>
class Array<void *>
{
private:
void* fBufferForElements;
public:

bool insert( const void* aElement, int aPos );

template <>
void xchg<string>( string& x, string& y )
{

string t = x;
x = y;
y = t;
}
Array<int> aIntegerArray;
sort<int, 10>( aIntegerArray );
113

Non-type Template Parameters

Non-type parameters are constant values that can be used


to specify certain limits (e.g., number of elements):

template <class Type, int s>


class Array
{
private:
Type fBufferForElements[s];
public:

bool insert( const Type& aElement, int aPos );

template <class Type, int n>


void sort( Array<Type>& a )
{

// Only sort up to position n.


}

114

Class Template Stack


namespace std
{
template <class T,
class Container = deque<T> >
class stack;
}
#include <stack>
using namespace std;
stack<int> st;
st.push( 1 ); st.push( 2 ); st.push( 3 );
while ( !st.empty() ) { cout << st.top() << endl; st.pop(); }

115

Class Template Map


namespace std
{
template <class Key,
class T,
class Compare = less<Key>,
class Allocator = allocator<pair<const Key, T> >
class map;
}

116

Class Template Set

namespace std
{
template <class T,
class Compare = less<T>,
class Allocator = allocator<T> >
class stack;
}

117

Defining a Comparer
#include <functional>
using namespace std;
template <class T>
struct MyLess : public binary_function< T, T, bool >
{
bool operator()( T aLeft, aRight );
}

118

Вам также может понравиться