Some notes:
Well, you have completed the wave of the procedural programming then object oriented programming. In order to complete your C and C++ journey, this Module will introduce you the generic programming.
This Module just to introduce you what the template is. The main task is to learn how to use and manipulate the STL components later.
This Module may be very useful if you want to create our own template but at the same time it should provide us with a good understanding of the template itself.
Compiler used in this Module is Visual Studio .Net® 2003 because many of the C++ features do not supported by some of the lousy compilers includedVisual Studio / C++ 6.0® with Service Pack 6 (SP6) :o)g++ (GNU C++) example is given at the end of this Tutorial. With g++, you will be warned if there are outdated and danger constructs in your programs :o).
The moral of this very short story is: if you want to develop programs that use many of the C++ features and don’t want to get cheated by the compiler, use fully ISO/IEC C++ compliance compiler.
You can see that many times naïve programmers have been cheated by the compiler! You think there is something wrong with your codes, but the compiler itself does not have the capabilities to understand your codes :o) or your codes really have bugs :o).
This Module and that follows supposed to make our tasks in programming smoother, easier, safer and more productive :o).
Get a good understanding of the templates so that it is easier for you to learn how to use them in the next Tutorials.
Source code for this tutorial is available inC++ STL Template source code.
| 24.1 Introduction
List<any_data_type>
List<int> List<name> List<messages>
List<any_data_type>
|
24.2 Function Template
Use to perform identical operations for each type of data.
Based on the argument types provided in the calls to function, the compiler automatically instantiates separates object code functions to handle each type of call appropriately.
The STL algorithms for example, are implemented as function templates.
A function template declaration contains the keyword template, followed by a list of template parameters and function declaration. The definition of a function template should follow its declaration immediately as opposed to normal functions.
For examples:
#include <iostream>
using namespace std;
// function declaration and definition
template <class any_data_type>
any_data_type MyMax(any_data_type Var1, any_data_type Var2)
{
// if var1 is bigger than Var2, then Var1 is the maximum
return Var1 > Var2 ? Var1:Var2;
}
If you have noticed, other than the red color line of code, it is same as normal function.
Using function templates is similar to normal function. When the compiler sees an instantiation of the function template, for example, the call of theMyMax(10, 20) in functionmain(), the compiler generates a functionMyMax(int, int).
Hence, it should be similar for other data type such as MyMax(double, double) and MyMax(char, char).
#include <iostream>
using namespace std;
// function template declaration and definition
template <class any_data_type>
any_data_type MyMax(any_data_type Var1, any_data_type Var2)
{
return Var1> Var2 ? Var1:Var2;
}
int main()
{
cout<<"MyMax(10,20) = "<<MyMax(10,20)<<endl;
cout<<"MyMax('Z','p') = "<<MyMax('Z','p')<<endl;
cout<<"MyMax(1.234,2.345) = "<<MyMax(1.234,2.345)<<endl;
// some logical error here?
cout<<"\nLogical error, comparing pointers not the string..."<<endl;
char* p = "Function";
char* q = "Template";
cout<<"Address of *p = "<<&p<<endl;
cout<<"Address of *q = "<<&q<<endl;
cout<<"MyMax(\"Function\",\"Template\") = "<<MyMax(p,q)<<endl;
cout<<"Should use Specialization, shown later..."<<endl;
return 0;
}
A class template definition look likes a regular class definition, except the keywordtemplate and the angle brackets< >.
It is declared using the keyword template, followed by a template parameter list enclosed in angle brackets and then a declaration and/or a definition of the class.
For example, the following code segment is a class template definition forMyStack. This is not a STL'sStack but our non standard stack template presented just for discussion.
If you notice, other than the red color line of code, it is same as normal class, isn’t it?
template <class any_data_type>
class MyStack
{
private:
// number of the stack’s element
int size;
// top position
int top;
// data type pointer
any_data_type* StackPtr;
public:
// constructor...
MyStack(int =10);
// destructor...
~MyStack(){delete [ ] StacKPtr;}
// put in data...
int push(const any_data_type&);
// take out data...
int pop(any_data_type&);
// test the emptiness...
int IsEmpty() const {return top == -1;}
// test the fullness...
int IsFull() const {return top == size – 1;}
};
|
template <class any_data_type>
class Vector
{
private:
any_data_type *buffer;
// copy constructor
Vector<any_data_type> (const Vector <any_data_type> &Var1)
//overloaded assignment operator
Vector<any_data_type>& operator=(const Vector<any_data_type>& Var2)
// destructor
~Vector<any_data_type>();
// other member functions…
any_data_type& operator [ ] (unsigned int index);
const any_data_type& operator [ ] (unsigned int index) const;
}
As in class definition, member functions for class template also can be defined outside the class body. For example:
// destructor definition
template <class any_data_type>
MyStack<any_data_type >::~MyStack()
{delete [ ] StackPtr;}
Or
// constructor definition
template <class any_data_type>
MyStack<any_data_type>::MyStack()
any_data_type is data type template parameter and it can be any data type. It acts as a placeholder for future use; currently its types are not yet specified. For example:
MyStack<MyClass>
WhereMyClass is user defined class. any_data_type also does not have to be a class type or a user defined type as shown in the following example.
MyStack<float>
MyStack<MessagePtr*>
Or in totally generic form as shown below. Keep in mind that the typename is a keyword in C++ and explained at the end of this Module.
template <typename any_data_type>
class Vector
{ }
A template can take one or more type parameters which are the symbols that currently represent unspecified types. For example:
template <class any_data_type>
class Vector
{ }
Here,any_data_type is a template parameter, also referred as type parameter.
Another example:
template <class any_data_type, int p>
class Array
{ }
Theany_data_type and p are template parameters.
When an ordinary type is used as parameter, the template argument must be a constant or a constant expression of an integral type. For example:
int num = 100;
const int Var1 = 10;
// should be OK, Var1 is a const
Array<float, Var1> Test;
// should be OK, 10 is a const
Array<char, 10> Test1;
// should be OK, constant expression been used
Array<unsigned char, sizeof(float)> Test2;
// not OK, num is not a constant
Array<int, num> Test3;
Besides the constant expressions, the only other arguments allowed are a pointer to a non-overloaded member, and the address of an object or a function with external linkage.
A template can take a template as an argument. For example:
int ReceiveMsg(const Vector<char*>&);
int main()
{
// a template used as an argument
Vector <Vector<char*> > MsgQ(20);
// other codes….
// receive messages
for(int j = 0; j < 20; j++)
ReceiveMsg(MsgQ[j]);
return 0;
}
Notice the space between the right two angle brackets, it should be mandatory as shown below to avoid the misinterpret of the right shift operator>>.
Vector <Vector<char*> > MsgQ(20);
Class template can have default type argument same as normal class. It provides flexibility for programmer to suit her/his needs. For example the STL Vector class template, the default type size_t is used but the programmer is free to choose other suitable data types instead.
template <class any_data_type, class S = size_t>
class Vector
{ };
// second argument default to size_t
Vector <int> TestVar;
Vector <int, unsigned char> short(7);
Another example of the default template argument:
template <class any_data_type = float, int element = 10>
MyStack{ };
Hence, the following declaration:
MyStack<> Var1;
Would instantiate at compile time a 10 element MyStack template class named Var1 of typefloat. This template class would be of type:
MyStack<float, 10>
For template specialization (will be discussed later), default arguments cannot be specified in a declaration or definition. For example:
#include <iostream>
using namespace std;
// primary template with default parameter
template <class any_data_type, int size>
class MyStack
{ };
// specialization declaration and definition
// with default arguments will generate error...
template <class any_data_type, int size=100>
class MyStack<int,100>
{ };
// do some testing
int main()
{
MyStack<float,100> Var1;
return 0;
}
Let try a simple template program skeleton.
// a simple class template program example
// ------declaration and definition part--------
template <class any_data_type>
class MyStack
{
private:
// number of the stack's element
int size;
// top position
int top;
// data type pointer
any_data_type* StackPtr;
public:
// constructor...
MyStack(int =10);
// destructor...
~MyStack(){delete [] StacKPtr;}
// put in data...
int push(const any_data_type&);
// take out data...
int pop(any_data_type&);
// test the emptiness...
int IsEmpty() const {return top == -1;}
// test the fullness...
int IsFull() const {return top == size - 1;}
};
// the main() program
int main()
{
return 0;
}
Member functions of non template classes may be templates. However, member templates cannot be virtual, nor may they have default parameters. For example:
// normal class
class MyClass
{
//...
// but, have template member function...
template <class any_data_type>
void MemberFunct(any_data_type)
{ };
};
int main()
{
return 0;
}
Here,MyClass::MemberFunct() declares a set of member functions for parameters of any type. You can pass any argument as long as its type provides all operations used by MemberFunct().
Nested classes may also be templates as shown below:
template <class any_data_type>
class MyClass
{
// ...
// nested class template
template <class another_data_type>
class NestedClass
{ };
// ...
};
int main()
{ return 0; }
tenouk fundamental of C++ object oriented tutorial
Source code for this tutorial is available inC++ STL Template source code.
Acomplete C++ Standard Library documentation that includes STL.
Check thebest selling C / C++, STL and UML books at Amazon.com.