C ++ (5)

Source: Internet
Author: User

Clause 47 template localized features

Function templates cannot be localized, even if they are reloaded.

However, the class template can be localized.

Template Class Heap; // master Template

Template ClassHeap {...}; // Localized features

The syntax for local features is similar to full features, but its template parameter list is not empty. When a Heap is instantiated using any (unmodified) pointer type, this localized version will take precedence over the master template. Further, when the real parameter type of the template is const char * or char *, the Heap of the fully-specific versions of const char * and char * will take precedence over this local feature.

The full or partial features of the master template must be instantiated using real parameters of the same quantity and type as the master template. However, the parameter list of the master template does not need to be in the same form as that of the master template. For Heap, the master template has a single type name parameter. Therefore, all full or partial features of Heap must be instantiated by using a single type name real parameter.

Template <> class Heap {...}; // Completely unique, with a single type name Template

// Real parameters, but the template parameter list is different from the main template parameter list. The fully-specific template parameter is empty.

Template ClassHeap {...}; // Localized, with a single type of real parameter, the template parameter // number list can also contain a single type of name parameter, but not required, as shown below

Template ClassHeap {...}; // Localized, with a single type of arguments, but this parameter must be an array with n elements of the T Type

Article 48 template member Specialization

For the main template, the full and partial features of the class template are completely separate entities, and they do not "inherit" any interfaces or implementations from the main template. But it is usually expected to have the same interface as the main template.

Template

Class Heap

{

Public:

Void push (const T & val );

};

You can select template-specific functions instead of template-specific functions.

Template <>

Void Heap : Push (constchar * const & pval ){...}

Note that the interfaces of these functions must exactly match the corresponding interfaces of the membership-specific template pair. For example, the main template declares push as a parameter with a type of const T &. Therefore, the explicit and special real parameters of push for const char * must be const char * const &.

If the Heap for General pointers already exists,

Template

Class Heap

{

Void push (T * pval );

};

If this local feature already exists, the explicit feature of push must comply with the push Member interface in this local feature, because this function is equivalent The result of Instantiation. Therefore, explicit features must be declared:

Template <>

Void Heap : Push (const char * pval ){...}

Cla49 use typename to eliminate ambiguity

You can use the tepename keyword to clearly tell the compiler that the following qualified name is a type name, which allows the compiler to parse the template correctly. This keyword is often used to describe the nested type.

Template 50 member Template

A member template is a member of the template.

Template

Class Slist {

Public:

//...

Template Slist (In begin, In end );

};

Template

Template

SList : SList (In begin, In end): head _ (0 ){

While (begin! = End)

Push_front (* begin ++ );

Reverse ();

}

Clause 51 use template to eliminate ambiguity

The rebind In the std Configurator is a template. An error occurs when the following definition is used:

Template >

Class SList {

//...

StructNode {

//...

};

TypedefA: rebind : Other NodeAlloc; // syntax error!

//...

};

Solution:

Typedef typename A: template rebind : OtherNodeAlloc;

Using the keyword template tells the compiler that rebind is a template, while using typename tells the compiler that the entire heap represents a type name.

Clause 52 special targeting type information

You can export type attributes from a specific version. Let's look at a simple example:

Template

Struct IsInt // T indicates an int

{

Enum {result = false };

};

Template <>

Struct IsInt // Unless it is an int

{

Enum {result = true };

};

With this main template and the fully-specific version, you can ask the compiler whether an unknown type is indeed int:

Template

Void aFunc (X & arg ){

//...

... IsInt : Result...

//...

}

Section 53 embedded type information

Template

Class Seq {

Public:

TypenameT Elem; // element type

TypenameT Temp; // temporary object type

Size_tsize () const;

//...

};

The embedded information can be queried during compilation:

Typedef Seq Strings;

//...

Strings: Elem aString;

Clause 54 traits

See Region

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.