27. c ++ primer 4th notes, templates and generic programming (2)

Source: Internet
Author: User
Tags export class

1When the return value of a function must be different from all types used in the form parameter table, it is necessary to overwrite the real parameter Inference Mechanism of the template and explicitly specify the type or value used by the template parameter.

Example

 
Sum (static_cast <int> (s), I );

2One way to specify the return type is to introduce the third template real parameter:

Example

Template <class T1, class T2, class T3> T1 sum (T2, T3); // poor design: Users must explicitly specify all three template parameterstemplate <class T1, class T2, class T3> T3 alternative_sum (t2, T1); int main () {// OK T1 explicitly specified; T2 and T3 inferred from argument typeslong val3 = sum <long> (I, LNG); // OK: CILS long sum (INT, long) // error: Can't infer initial template parameterslong val3 = alternative_sum <long> (I, LNG ); // OK: all three parameters explicitly specifiedlong val2 = alternative_sum <long, Int, long> (I, LNG); return 1 ;}

The instances that provide explicit templates for calling and participate in defining class templates are similar. The parameters of the explicit template are matched from left to right, and the first template is involved in the matching of the first template parameters, and so on. If it can be inferred from the function parameter, the explicit template real parameter of the ending (rightmost) parameter can be omitted

3, Function template pointer assignment

Example

Template <typename T> int compare (const T &, const T &); // overloaded versions of func; each take a different function pointer typevoid func (INT (*) (const string &, const string &); void func (INT (*) (const Int &, const Int &); func (compare <int> ); // OK: explicitly specify which version of compare

4Template compilation Model

1) Including the compilation Model

In this model, the compiler must be able to see the template definitions used. It depends on the template definition being included in each file using the template. Generally, the template definition is stored in a header file. Any file using the template must contain the file.

2) Compile models separately.

The compiler is used to find the template definition mechanism. It allows the template definition and declaration to be stored in an independent file. The template declaration is placed in a header file, and the definition is only inProgramAppears once

PassExportKeyword to implement this mechanism.ExportIs a keyword used to indicate that the compiler must remember the location of the template definition. ExportThe keyword usually appears together with the function definition (the Declaration does not need to be specifiedExport,Class definitions in header files should not be usedExportIf you useExport,The header file can only be used by one source file in the program) The class is usually declaredExport. In a program, a template can only be usedExportThe keyword is defined once.

The export class members are automatically exported. You can also declare individual members of the class template as exported. The definition of any non-exported member function must be the same as that in the include model: The definition should be placed in the header file of the definition class template.

Example

 
// Class template header goes in shared header filetemplate <class type> class queue {//...}; // queue. CC implementation file declares queue as exportedexport template <class type> class queue; # include "queue. H "// queue member Definitions

5Generally, when using the class Template Name, you must specify the template parameters. This rule has an exception:Within the scope of the class, you can use the non-qualified name of the class template.. For example, in the default constructor and

In the Declaration of the copy constructor, the name isQueueYesQueue <type>Abbreviation. Essentially,Compiler InferenceWhen we reference the class name, it references the same version.

The compiler does not make such inferences for the template parameters of Other templates used in the class.

6Member functions defined outside the class

Example

 
Template <class T> ret-type queue <t>: Member-name;

The member functions of the class template are also function templates. Unlike other function templates, when instantiating class template member functions, the compiler does not execute template real-parameter inference.

The real parameter of a non-type template must be a regular expression for compiling.

7And the membership statement in the class template.

Three types:

1) For normal non-template classes or functions, assign the membership relationship to explicitly specified classes or functions.

2.

3) Only the membership statement that grants access to a specific instance of the class template or function template.

Example

{//... // The first situationfriend class foobar; friend void FCN (); // The second situationtemplate <class T> friend class foo1; template <class T> friend void templ_fcn1 (const T &); // The third situationfriend class foo2 <char *>; friend void templ_fcn2 <char *> (char * const &); //...} // The following forms are more common: Template <class T> class foo3; Template <class T> void templ_fcn3 (const T &); template <class type> class bar {// each instantiation of bar grants access to the // version of foo3 or templ_fcn3 instantiated with the same typefriend class foo3 <type>; friend void templ_fcn3 <type> (const type &);//...}; bar <int> Bi; // foo3 <int> and templ_fcn3 <int> are friendsbar <string> BS; // foo3 <string>, templ_fcn3 <string> are friends

In the preceding example ,,Foo3 <int>AccessibleBar <int>But cannot access

Bar <string>Or any otherBarPrivate part of the instance.

Declared dependencies:

When you grant access to an instance of a given template, you do not need to declare this type of template or function template in the scope. In essence, the compiler treats a friend Declaration as a class or function declaration.To limit the relationship between friends and friends of a specific instantiation, you must(Can Use)Declare a class or function before you declare it.

ExampleCode

 
Template <class T> Class A; Template <class T> Class B {public: Friend Class A <t>; // OK: A is known to be a templatefriend Class C; // OK: C must be an ordinary, nontemplate classtemplate <class S> friend Class D; // OK: D is a templatefriend Class E <t>; // error: e wasn't declared as a templatefriend Class F <int>; // error: F wasn't declared as a template };

If you do not tell the compiler that the friend is a template, the compiler considers the friend as a common non-template class or non-template function.

8Any class (template or non-template) can have members of its own class template or function template. Such members are calledMember template. The member template cannot be virtual..

9When a member template is a member of a class template, its definition must contain the class template parameters and its own template parameters.

Sample Code

 
Template <class T> template <class ITER> void queue <t>: Assign (ITER beg, ITER end) {// destroy (); // remove existing elements in this queue // copy_elems (beg, end); // copy elements from the input range}

10The member templates follow the regular access control.

11The member template function 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.

12Every instantiation of the template class has its ownStaticMember. Like any other member function,StaticMember functions are instantiated only when used in programs. Like using any otherStaticSimilar to data members, data member definitions must appear outside the class.

Example

Template <class T> size_t Foo <t>: CTR = 0; // define and initialize CTR

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.