Chapter 5 templates and generic programming (15)

Source: Internet
Author: User

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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.