Why do the declaration and definition of function templates and class templates in C + + need to be put together?

Source: Internet
Author: User

It is not elegant to write the declaration and definition of a template together. Try to use the "traditional" method, and declare it in the. h file, defined in the. cpp file,

Then include the. h header file in the main function, which will report a link error. why!!!!!!!!!!!!!

This is because the function template is instantiated to become a real function, including the header file of the function template in the source file that uses the function template .

If there is only a declaration and no definition in the header file, the compiler cannot instantiate the template, resulting in a link error. (class template same!!) )

1 //---------------test.h-------------------// 2  voidf ();//this declares a function f3 //---------------test.cpp--------------// 4 #include "test.h"5  voidf ()6  { 7...//Do something8}//this implements the F function declared in the test.h9 //---------------main.cpp--------------// Ten #include "test.h" One  intMain () A  {  -f ();//Call F -}

At compile time, two obj files are generated, main.obj and Test.obj, and in main.obj there is no binary code for the F function, which actually exists in test.obj. The call to F in Main.obj will generate only one row of calls, the address of which is generated by the linker.

1 //-------------test.h----------------// 2template<classT>3  classA4  { 5      Public: 6      voidf ();//This is just a statement .7  }; 8 //---------------test.cpp-------------// 9 #include "test.h"Tentemplate<classT> One  voidA<t>:: F () A  {  -...//Do something -  }  the //---------------main.cpp---------------//  - #include "test.h" -  intMain () -  {  +a<int>A; - A. f (); +}

We know that the template has a process of being active, and it doesn't generate binaries when it's not being used. So when the linker goes to the address of the F function, because it has not been called before F (), Test.obj does not have the F function binary code, so it will be an error.

There is no way to separate the template declaration from the definition.

The first option is to include the CPP file in the main function.

1 //-------------test.h----------------// 2template<classT>3  classA4  { 5      Public: 6      voidf ();//This is just a statement .7  }; 8 //---------------test.cpp-------------// 9 #include "test.h"Tentemplate<classT> One  voidA<t>:: F () A  {  -...//Do something -  }  the //---------------main.cpp---------------//  - #include "test.cpp"//careful!!!!!!!!!  -  intMain () -  {  +a<int>A; - A. f (); +}

So the contents of the three files are actually contained in the same file through include, and naturally there is no error.

1 //-------------test.h----------------// 2template<classT>3  classA4  { 5      Public: 6      voidf ();//This is just a statement .7  }; 8#include <test_impl.h>
9 //---------------test_impl.h-------------// Tentemplate<classT> One voidA<t>:: F () A { -...//Do something - }
the //---------------main.cpp---------------// - #include "test.h" - intMain () - { +a<int>A; - A. f (); +}

Both of these methods actually contain compilation, no essential difference, but the second method seems to be more comfortable

1 //-------------test.h----------------// 2template<classT>3  classA4  { 5      Public: 6      voidf ();//This is just a statement .7  }; 8 //---------------test.cpp-------------// 9 #include "test.h"Tentemplate<classT> One  voidA<t>:: F () A  {  -...//Do something -  }  theTemplateclassa<int>;//!!!!!!!!!! In this implementation of the present type so that the compilation will not have a problem but it's not so good to think why!!!  - //---------------main.cpp---------------//  - #include "test.h" -  intMain () +  {  -a<int>A; + A. f (); A}

That's it. OK is actually looking at someone else's blog!

Why do the declaration and definition of function templates and class templates in C + + need to be put together?

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.