C ++ templates Study Notes (5) -- Chapter 6 practical templates

Source: Internet
Author: User

1 Chapter 6 Template practice

In a sense, a template is a structure between a macro and a general declaration.

 

1.1 Include Model

We can organize templates in several waysSource code, The most common is to include the model.

There are three ways to organize a model:

LWrite the template class and template function directly in the header file using inline functions ;(InVs2005This model is currently supported.)

LThe declaration and definition are separated, but they are all written in the header file;

LPlace the template statements and definitions in two different files (that is. HAnd. Cpp), And then add it at the end of the header file.# Include "XXX. cpp"You can.

In general, we recommend the second method.

 

If you do not need to consider the creation time, we recommend that you use the include model to organize the template as much as possible.Code.

 

1.2 Explicit instantiation

The inclusion model ensures that all required templates have been instantiated. This is because: When instantiation is required,C ++The compilation system automatically generates the corresponding instantiation body.

C ++The standard also provides a mechanism to manually instantiate a template: display the instantiation indicator.

1.2.1 Display examples of Instantiation

Template VoidPrint_typeof <Double> (Double Const&);

 

The display instantiation indicator is composed of the keyword template and the declaration of the entity (which can be a class, function, or member function) that we need to instantiate next to it, in addition, this declaration is a statement that has completely replaced the parameters of real parameters.

Each object can only be instantiated once.

1.3 Separation Model

It's like definingC ++Class Definition, just before defining each member functionExportKeyword, but currently,Vs2005This keyword is not supported yet.

So ignore it for the moment. As for the preparation of the separation model mentioned in the book, the viewpoint is good, but it seems thatVs2005Cannot be compiled. Because if I place the Declaration and definition in two files, it will cause compilation errors.

 

1.4 Precompiled header file

The pre-compiled header file mechanism is outside the standard and mainly depends on the implementation of specific products.

For systems with sufficient memory, the pre-compiled header file mechanism will make the processing speed much faster than the compilation of most single standard header files.

You can manage the header file by layering the pre-compiled file, that is, layering Based on the Usage Frequency and Stability of the header file.

We should pre-compile those header files that belong to a more stable level, and then re-use this stable pre-compiled header file in the less stable header file to improve the overall compilation efficiency.

 

1.5 Debugging Template

This section is not useful to me, but it just introducesTracerIt is quite interesting. If you are interested, you can check it or debug some self-writtenAlgorithm.

 

 

1.6 Postscript

The inclusion model is actually used.C ++Most compiler implementations use this method.

 

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.