FAQ 25.02 What are the syntax and semantics for a class template?

The syntax of a class template is the keyword template, some template parameters, then something that looks a lot like a class. But semantically a class template is not a class: it is a cookie cutter to create a family of classes.

Consider a container class (see FAQ 2.15). In practice, the C++ source code for a container that holds ints is structurally very similar to the C++ source code for a container that holds strings. The resulting binary machine code is probably quite different, since, for example, copying an int requires different machine instructions than does copying a string. Trying to make the binary machine code the same might impose runtime overhead to generalize, for example, the copying operations for int and string and might also increase the complexity of the container.

Class templates give programmers another option: capturing the source code similarity without imposing extra runtime performance overhead. That is, the compiler generates special purpose code for containers of int, containers of string, and any others that are needed.

For example, if someone desired a container that acted like an array, in practice they would probably use the standard class template vector<T>. However, for illustration purposes we will create a class template Array<T> that acts like a safe array of T.

 template<class T>                                    <-- 1 class Array { public:   Array(unsigned size=10);   Array(const Array<T>& a);                //copy constructor   Array<T>& operator= (const Array<T>& a); //assignment  ~Array() throw();   unsigned size() const throw();   const T& operator[] (unsigned i) const throw(out_of_range);   T&       operator[] (unsigned i)       throw(out_of_range); protected:   unsigned  size_;   T*        arr_; }; template<class T> inline Array<T>::Array(unsigned size) : size_(size) , arr_(new T[size]) { } template<class T> inline Array<T>::~Array() throw() { delete[] arr_;  } template<class T> inline unsigned Array<T>::size() const throw() { return size_; } template<class T> inline const T& Array<T>::operator[] (unsigned i) const throw(out_of_range) {   if (i >= size_) throw out_of_range("Array<T>::operator[]"));   return arr_[i]; } template<class T> inline T& Array<T>::operator[] (unsigned i) throw(out_of_range) {   if (i >= size_) throw out_of_range("Array<T>::operator[] const");   return arr_[i]; } 

(1) T is a type in the declaration that follows

The template<class T> part indicates that T represents a yet unspecified type in the class template definition. Note that the keyword class doesn't imply that T must be a user-defined type; it might be a built-in type such as int or float.

The C++ Standard defines the term instantiated class to mean the instantiation of a class template, but we will use the term instantiation of a class template instead, since most C++ programmers think of an instantiated class as an object rather than another class. When it doesn't matter whether it is a class template or a function template, we will drop the qualifying adjective and refer to the instantiation of a template.

 #include <string> #include "Array.hpp" using namespace std; int main() {   Array<int>    ai;  ai[5] = 42;   Array<float>  af;  af[5] = 42.0;   Array<char>   ac;  ac[5] = 'x';   Array<string> as;  as[5] = "xyz"; } 

Normally the compiler creates an instantiation of a class template when the name of a class template is followed by a particular sequence of template arguments. In this case, the only template argument is a type. The compiler generates code for the instantiated template by replacing the template argument T with the type that is supplied, such as int.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net