16.1 template Definition

Source: Internet
Author: User

16.1.1 function template.

The template definition starts with the keyword template. The following is a template parameter table. The template parameter table contains one or more parameter lists in angle brackets. The parameters are separated by commas.

A template parameter can be a type parameter or a non-type parameter that represents a constant expression.

Use the function template.

When using a function template, the compiler determines which or which template parameters are bound to the template parameters.

Once the compiler determines the corresponding template parameters, it is called: instantiate a function template instance.

Inline Function Template

Template <typename T> inline T min (const T &, const T &);

Note: The Inline specifier is placed after the template parameter and prior to the type returned by the function.

16.1.2 define a class template.

You can define a class template like a function template.

Use of class templates.

Different from the function template's automatic pushing and exporting of real parameter types by compilation. When using a class template, you must explicitly specify the real parameters for the template parameters.

For example, queue <int> qi;

Queue <vector <double> QC;

The compiler uses the real parameters of the template to instantiate this class.

16.1.3 template parameters.

Template parameter scope: the name of the template parameter. It can be declared as a template parameter and used at the end of the template declaration or definition.

Template <typename T> // T.

Class democlass

{

......

}; // T scope.

The template parameters follow the regular name blocking rules. The name of the inner layer shields the outer layer.

Template parameters with the same name as the objects, functions, or types declared in the global scope block their global names.

Typedef double T; Template <class T> T calc (const T & A, const T & B) // here the template parameter t shields the global typedef {// TMP has the type of the template parameter t // not that of the global typedef t TMP = ;//... return TMP ;}
16.1.4, the type parameter of the template.
Type parameters are composed of class or typename followed by specifiers.
This type parameter can be used as a type specifier anywhere in the template, just like built-in type specifiers (such as, Int, double)
Specify the type within the template definition.
We know that the class can define data members, function members, and type members. For example, size_type in STL.
When we want to use a type member, we should explicitly specify that this name is a type.
Template <class parm, Class U> parm FCN (parm * array, U value) {parm: size_type * P; // If parm: size_type is a type, then a declaration // If parm: size_type is an object, then multiplication} For the above usage, the compilation will assume that the size_type variable is multiplied by the other P.
We should explicitly tell the compiler that size_type is a type here.
By adding the keyword typename before the type member as the prefix, You can explicitly tell that the name is a type.
For example
     template <class Parm, class U>     Parm fcn(Parm* array, U value)     {         typename Parm::size_type * p; // ok: declares p to be a pointer     }

Here, typename parm: size_type tells the compilation that size_type is a type name,

Typename parm: size_type * P defines a pointer to the parm: size_type type.

Of course, the premise is that the parm class we use here has a size_type member.

16.1.5 non-type template parameters.

The template parameter does not need to be of type. It can also be non-type.

1. Function template. When a function is called, non-type parameters are replaced by values. The value type is specified in the template parameter table.

     // initialize elements of an array to zero     template <class T, size_t N> void array_init(T (&parm)[N])     {         for (size_t i = 0; i != N; ++i) {             parm[i] = 0;         }     }

N is a non-type parameter. The real parameters of N are calculated by the compiler.

WhenArray_initThe compiler calculates the value of a non-type parameter from the array arguments:

     int x[42];     double y[10];     array_init(x);  // instantiates array_init(int(&)[42]     array_init(y);  // instantiates array_init(double(&)[10]

A non-type parameter is a constant value within the template definition.

When the template definition requires a constant value to be determined, you can use a non-type parameter.

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.