C ++ learning note 34 template principle, learning note 34

Source: Internet
Author: User

C ++ learning note 34 template principle, learning note 34

Templates play a very important role in C ++. STL is written in large quantities using templates.

I will not list the advantages of templates. Here I will only talk about the principles of the template.

When the compiler encounters a template method definition, the compiler checks the syntax, but does not compile the template. The compiler cannot compile template definitions because the compiler does not know what type to use and cannot generate code for statements like x = y without knowing the types of x and y.

When the compiler encounters an instantiated template, such as vector <int> vi (here I just take the vector as an example. In fact, the basic type of vector code seems to automatically exist in the compiler ), the compiler replaces each T in the template class definition with an int to generate the int version of the vector. When the template encounters another instance of this template, such as vector <string>, the corresponding string version code is generated.


1. Selective instantiation.

The compiler only generates code for class methods actually called for a certain type.

#include <iostream>using namespace std;template<class T>class test{private:T data;public:test(T d):data(d){}void show()const{cout<<"this data is "<<data<<endl;}};int main(){test<int> ti(15);ti.show();test<string> ts("string");ts.show();}

For example, in the preceding example, the compiler only generates the int and string versions of test, while other versions such as double and char do not.




Do you have any C Language Study Notes or prepared some documents?

Hundred examples of C language programming: it is easier to learn from examples. It is better than simply reading those books with principles //
Is it a procedural test or a theoretical test?
Recommended program design guidance and online practices for trial use, outline-level books. Link: ai.pku.edu.cn/book/
The theoretical written examination must be dominated by Tan haoqiang, but it is indeed a bit messy. You may wish to buy a related exercise or something like this. Tan haoqiang has many related exercises in this book, which are basically the same, choose based on the existing materials on hand. We will not recommend it.

What are the principles of cms labels and templates?

CMS labels are used to better call the defined tag-related functions. For example, if the column ID is 1, a = 1. What is the concept of a template? That is, you can configure images, CSS, web files, and other files of the template separately and put them in other programs for practical use. Modify the CMS tag in the template. If the original ID is 5 and you have set a column to 6, you can change the corresponding content in the 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.