Three ways to organize C + + template source code

Source: Internet
Author: User

There is a difference between template code and non-template code, if the template's declaration is placed in the header file like non-template code. h, the template definition is placed in the source file. cpp, then you get a link error when using this template. The reason for this error is that the definition of the template has not yet been instantiated. In order to instantiate a template, the compiler must know which definition should be instantiated and what template parameters to use to instantiate it.

We can organize the template code in three ways:

1. Inclusion model (inclusion Modal)

A. Include the definition of the template in the header file of the declaration template, and if the template is declared in the header file tmpl.h, defined in Tmpl.cpp, you can add # include "Tmpl.cpp" to the end of the tmpl.h file.
B. or include the Tmpl.cpp file in each C + + file that uses the template to achieve the purpose.
C. Of course, you can also use the. cpp file to place template declarations and definitions in the same. h file.
The inclusion model ensures that all required templates are instantiated because the C + + compilation system automatically generates the corresponding instantiation when it needs to be instantiated.

2. Display instantiation (Explicit instantiation)

An explicit instantiation consists of a keyword template and a declaration that is immediately followed by the entity that we want to instantiate (which can be a class, function, class member function, and so on), and that Declaration is a declaration that has been completely replaced with an argument.
A. Template const int& MAX (const int& A, const int& B); An explicit instantiation of a function template
B. Template class array<char>; Explicit instantiation of class templates
C. template unsigned int array<char>::getsize (); Explicit instantiation of class template member functions

3. Separation model (separation Modal)

The C + + standard also gives another mechanism for organizing template code: The Export Template (exporting templates), which is often referred to as a separation model for C + + templates.
The detach model only needs to add the Export keyword in the. h file of the template declaration, such as:
Export Template <typename t> void func (const t&);
However, the VC + + compiler does not support the separation model so far.

Three ways to organize C + + template source code

Related Article

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.