16.4.5 member Template
Any class (template or non-template) can have its own members as class templates or function templates. Such members are called member templates, and member templates cannot be virtual.
1. Define a member Template
Template <class Type>
Class Queue {
Public:
Queue (): head (0), tail (0 ){}
Queue (const Queue & Q): head (0), tail (0)
{Copy_elems (Q );}
Queue & operator = (const Queue &);
~ Queue () {destroy ();}
Type & front ()
{Return head-> item ;}
Const Type & front () const {return head-> item ;}
Void push (const Type &);
Void pop ();
Bool empty () const
{Return head = 0 ;}
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );
Template <class It>
Queue (It beg, It end): head (0), tail (0) {copy_elems <It> (beg, end )}
Template <class Iter>
Void assign (Iter, Iter );
Private:
QueueItem <Type> * head;
QueueItem <Type> * tail;
Void destroy ();
Void copy_elems (const Queue &);
Template <class Iter>
Void copy_elems (Iter, Iter );
};
Template <class Type>
Class Queue {
Public:
Queue (): head (0), tail (0 ){}
Queue (const Queue & Q): head (0), tail (0)
{Copy_elems (Q );}
Queue & operator = (const Queue &);
~ Queue () {destroy ();}
Type & front ()
{Return head-> item ;}
Const Type & front () const {return head-> item ;}
Void push (const Type &);
Void pop ();
Bool empty () const
{Return head = 0 ;}
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );
Template <class It>
Queue (It beg, It end): head (0), tail (0) {copy_elems <It> (beg, end )}
Template <class Iter>
Void assign (Iter, Iter );
Private:
QueueItem <Type> * head;
QueueItem <Type> * tail;
Void destroy ();
Void copy_elems (const Queue &);
Template <class Iter>
Void copy_elems (Iter, Iter );
}; 2. Define a member template outside the class
When defining a member template outside the scope of the class template, it must contain two template parameters.
Template <class Type>
Template <class Iter>
Void Queue <Type >:: assign (Iter beg, Iter end ){
Destroy ();
Copy_elems (beg, end );
}
Template <class Type>
Template <class Iter>
Void Queue <Type >:: assign (Iter beg, Iter end ){
Destroy ();
Copy_elems (beg, end );
} Because the assign function deletes members from an existing container, it is necessary for the iterator passed to the assign function to reference elements in different containers. The assign members of the standard container have the same restrictions as the iterator constructor.
3. The member template follows regular access control
The member template follows the same access rules as any other class members.
4. Member templates and instantiation
Like other members, a member template is instantiated only when used in a program. A member template has two template parameters: defined by the class and defined by the member template itself. The class template parameters are determined by the type of the object that calls the function. The behavior of the member-defined template parameters is the same as that of the common function template. These parameters are determined by the real parameters of the general template.
From xufei96's column