16.6.2 special templates
1. Define special classes
Template <>
Class Queue <const char *> {
Public:
String & front ()
{Return real_queue.front ();}
Const string & front () const {return real_queue.front ();}
Void push (const char *);
Void pop ();
Bool empty () const
{Return real_queue.empty ();}
Friend ostream & operator <const char *> (ostream & OS, const Queue <const char *> & q );
Private:
Queue <string> real_queue;
};
Template <>
Class Queue <const char *> {
Public:
String & front ()
{Return real_queue.front ();}
Const string & front () const {return real_queue.front ();}
Void push (const char *);
Void pop ();
Bool empty () const
{Return real_queue.empty ();}
Friend ostream & operator <const char *> (ostream & OS, const Queue <const char *> & q );
Private:
Queue <string> real_queue;
};
Specialization can be defined as a completely different member from the template in this province. If a special object cannot define a member from the template, the object of this special type cannot use this member. The class template member definition is not used to create an explicit special member definition.
Class template specialization should be the same interface as the template definition that it is specific to. Otherwise, users may be surprised when trying to use undefined members.
2. Class-specific definition
When a class-specific external definition member is added, the member cannot be marked with template <>.
Void Queue <const char *>: push (const char * val)
{
Return real_queue.push (val );
}
Void Queue <const char *>: push (const char * val)
{
Return real_queue.push (val );
}
16.6.3 special member without special class
Template <>
Void Queue <const char *>: pop ()
{
QueueItem <const char *> * p = head;
Delete head-> item;
Head = head-> next;
Delete p;
}
Template <>
Void Queue <const char *>: pop ()
{
QueueItem <const char *> * p = head;
Delete head-> item;
Head = head-> next;
Delete p;
} The declaration of member specialization is the same as that of any other function template. It must start with an empty template parameter table.
Template <>
Void Queue <const char *>: pop ();
From xufei96's column