C + + Template compilation model

Source: Internet
Author: User
Tags class definition function definition

One: the traditional compilation model

When you program in C + +, you typically use a header file to separate the definition from the declaration and make the program organized in a modular manner. Place the definition of a function declaration, a class in a header file, and the definition of a function implementation and a class member function in a separate file.

But for the template, this approach is not feasible, the specific examples are as follows:

The first is the header file that contains the template declaration temp.h:

Temp.h#ifndef temp_h#define temp_htemplate<typename t>int Compare (const t &a, const T &b);template< TypeName T>class testtemp{public:    testtemp (const T &a): M_value (a) {}    void display ();p rivate:    T   m_value;}; #endif

The header file contains a declaration of a function template and a definition of a class template.

Here is the source file temp.cpp that contains the template definition:

Temp.cpp#include <iostream> #include "temp.h" Template<typename t>int compare (const t &a, const T & b) {    if (a < b)  return-1;    if (b < a)  return 1;    return 0;} Template <typename t> void testtemp<t>::d isplay () {    std::cout << m_value << Std::endl;}

Here is the main function file main.cpp:

Main.cpp#include <iostream> #include "temp.h" int main () {    int a = 1, b = 3;    int res;    Testtemp<int> TT (4);    Tt.display ();    res = Compare (A, b);    Std::cout << "Res is" << res << Std::endl;}

  

When compiling an executable file for the above file compilation, an error is generated:

# g++-o main main.cpp temp.cpp/tmp/ccnwfo8x.o:in function ' main ': main.cpp: (. text+0x47): Undefined reference to ' testtemp <int>::d isplay () ' Main.cpp: (. text+0x5a): undefined reference to ' int compare<int> (int const&, int const &) ' Collect2:error:ld returned 1 exit status

The reasons for the error are as follows:

The amount of space that each object occupies in C + + is determined at compile time. In the compile phase, when the source file Main.cpp contains the header file containing the template declaration Temp.h is included, the compiler needs to generate the appropriate memory layout for each object involved in main.cpp, generating the corresponding instruction for each function.

When the source file main.cpp involves the invocation of a template class member function or a template function, because the template function is defined in another source file Temp.cpp, the compiler is now only aware of their declarations. So, the testtemp<int> that is called in main.cpp: the:d isplay function, and the int compare<int> (int const&, int const&) function, The compiler believes that the implementation of these functions is in other source files, the compiler will not error, because the connector will eventually connect all the binaries to complete the symbol lookup, to form an executable file.

Although the compiler also compiles the source file temp.cpp that contains the template definition, the file is simply the definition of the template, and there is no real instantiation of the specific function. Therefore, in the link stage, when the compiler is looking for symbols, found in the source code file symbols, in all binary files can not find the relevant definition, so the error.

Two: Template compilation model

When the compiler sees the template definition, it does not immediately generate code. The compiler produces a specific type of template instance only when it sees the use of a template, such as when a function template is called or an object that invokes a class template is called.

In general, when calling a function, the compiler only needs to see the declaration of the function. Similarly, when you define an object of a class type, the class definition must be available, but the definition of the member function does not have to exist. Therefore, class definitions and function declarations should be placed in the header file, while the definitions of the normal functions and class member functions are placed in the source file.

Templates are different: to instantiate, the compiler must be able to access the source code that defines the template. When you call a function template or a member function of a class template, the compiler requires a function definition that requires code that is normally placed in the source file.

Standard C + + defines two models for the compiled template code. Are the compilation model and the separately compiled model.

The so-called inclusion of the compilation model, plainly, is to put the definition of function template in the header file. Therefore, for the above example, the content of Temp.cpp is put into temp.h.

There is a problem with the compilation model, and if two or more separate compiled source files use the same template, the compilers produce one instance for each template in the file. So a given template produces multiple identical instances, and when linked, the compiler chooses one instantiation and discards the others.

In the separate compilation model, the compiler will track the relevant template definitions for us. However, we have to let the compiler know to remember the given template definition, so we need to use the Export keyword. However, many compilers do not support this keyword, and c++11 sets the keyword to unsued and reserved.

So, the conclusion is that the template definition and implementation are placed in the header file.

Reference:

Http://gaunthan.leanote.com/post/C-%E6%A8%A1%E6%9D%BF%E7%9A%84%E7%BC%96%E8%AF%91%E6%A8%A1%E5%9E%8B

https://www.zhihu.com/question/20630104

C + + Template compilation model

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.