28. c ++ primer 4th notes, templates and generic programming (3)

Source: Internet
Author: User

1, UseConst char *When the following template is called as a real parameter, the pointer value is compared, not the string to which the parameter is directed. In this case, the template must be customized.

Example

 
Template <typename T> int compare (const T & V1, const T & V2) {If (V1 <V2) Return-1; if (V2 <V1) return 1; return 0 ;}

2, Function template Special: the actual type or actual value of one or more template parameters is specified.

• KeywordsTemplateFollowed by an empty pair of angle brackets (<>);

• The Template Name and a pair of angle brackets are used to specify the specific template parameters;

• Function parameter table;

• Function body.

ExampleCode

// Special version of compare to handle C-style character stringstemplate <> int compare <const char *> (const char * const & V1, const char * const & V2) {return strcmp (V1, V2 );}

The special declaration must match the corresponding template.

1), Like any function, function templates can be customized without being defined.

Example

 
// Declaration of function template explicit specializationtemplate <> int compare <const char *> (const char * const &, const char * const &);

2), OmittedTemplate <>The result is that the overload of the function is not a template version.

Example

 
// Generic template definitiontemplate <class T> int compare (const T & T1, const T & T2 ){/*... * //} // OK: Ordinary Function declarationint compare (const char * const &, const char * const &);

When defining a non-template function, regular conversion is applied to the real parameters. When the Special template is used, conversion is not applied to the real parameters.

3), IfProgramIt consists of multiple files. The template Special Declaration must appear in each file that uses the special template.

3, Class template special

Example

/* Definition of specialization for const char ** this class forwards its work to queue <string>; * The push function translates the const char * parameter to a string * The Front functions return a string rather than a const char **/template <> class queue <const char *> {public: // No copy control: synthesized versions work for this class // Similarly, no need for explicit default constructor eithervoid push (const char *); void POP () {real_queue.pop ();} bool empty () const {return real_queue.empty ();} // Note: return type does not match template parameter typestd: String Front () {return real_queue.front ();} const STD:: string & Front () const {return real_queue.front ();} PRIVATE: queue <STD: String> real_queue; // forward callto real_queue }; void queue <const char *>: Push (const char * val) {return real_queue.push (VAL );}

When a class-specific external definition member is addedTemplate <>Mark.

1) In addition to the entire class template, only the members can be converted. However, the declaration of the special member must start with an empty template parameter table, just like any other function template.

Example

 
Template <> void queue <const char *>: Push (const char * const &);

2) Some features of the class template

Some of the class templates are also specific templates. The compiler selects the most specific template definition for the instantiation.

Example

 
Template <class T1, class T2> class some_template {//...}; // partial specialization: fixes T2 as int and allows T1 to varytemplate <class T1> class some_template <t1, int> {//...}; some_template <int, string> Foo; // uses templatesome_template <string, int> bar; // uses partial Specialization

4, Overload and function Template

Function templates can be overloaded: You can define multiple function templates with the same name but different parameter numbers or types, or define common non-template functions with the same name as the function template.

Common functions take precedence over template versions. When the matching is the same, non-template versions are preferred.

5The function template is set upAlgorithmLibrary basics. Class templates are the basis for creating standard library containers and iterators.

6And several key concepts:

1 ) generic handle class: class that saves and manages pointers to other classes. A generic handle accepts a single type parameter and assigns and manages pointers to this type of object. The handle class defines the necessary copy control members. *, -> operator. You do not need to know the types it manages.

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.