Implementation of C + + array class template

Source: Internet
Author: User





1. Preparation of knowledge


(1) The template parameter can be not only a type parameter, but also a numeric type parameter.


Such as:

Template

<typename T, int n>//int N is a numeric parameter, pay attention to this type of int, because there is a limit to the numerical parameters, and the following are the limitations of the numerical parameters.

void Func ()

{

T A[n]; //Use template parameters to define the local array, the template's numeric parameter to represent the size of the array.

}


Func<double, 10> (); //This is used, numeric parameters, that is, the second parameter if the constant, because this is the limit of the numeric template parameters caused.


Limitations of numeric template parameters: Variables can not be the most template parameters, floating-point numbers can not be used as template parameters, class objects can not be used as template parameters.


Essence: The template parameter is a unit that is processed at compile time, which is no doubt, because the compiler will first compile the template, so in the compilation phase must be accurate and unique, the variable is not able to be determined during the compilation phase, so the variable

Floating-point numbers cannot be used as template parameters because they are inaccurate in the computer's memory, so floating-point numbers are not allowed in C + + as template parameters. The class object cannot be uniquely determined at compile time because it is actually a variable and the class variable is placed.


(2) An interview question, you find the most efficient way to calculate the value of 1+ to N.


Template

< int N >//Numeric template parameter function class template

Class Sum

{

Public

static const int VALUE = Sum<n-1>::value + N;

};


Template

< > full specificity

Class sum< 1 >

{

static const int VALUE = 1;

};


int main (void)

{

cout << "1 + 2 + 3 + ... + + =" <<sum<100>::value <<endl;


return 0;

}


Explanation: In the above program, the class template uses a numeric parameter, which can be seen from the template parameter int, so when we use it, we pass the value directly. Value is const and has initialization, so it will be obtained during the compilation phase.

The value of this constant is placed in the symbol table, and value is modified by static, so it is placed in the global data area. When the compiler compiles to value, it first gets the value of the constant and puts it into the symbol table, but the compiler discovers

To get value, the first thing to get the value of N-1, to get the value of N-1, to get the n-1-1 value, know that n is 1, the compiler found that the class of the special, so use this special class, value is 1, because there are recursive procedures,

So in return, N constantly +1, each time the value of +1 will be added to value of the constant, until N changed to N, that is, added to the N, the last value is completely finished, and finally put in the symbol table.

This is an efficient summation method. Because the compiler at compile time, the value of the constant values have been determined, we later cout to access this value, just equivalent to directly from the symbol table to remove the value, so efficient.

This efficient approach relies on the technology of class template technology, full-scale class template technology, and also relies on the technique of class template parameters as numerical parameters, and also relies on the understanding of const and static. With static, you can use the scope resolution to access this value,

With const, the value is determined during the compilation phase, because the value of the constant is determined during the compilation phase.


Implementation of C + + array class template

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.