How to implement C + + template class header file and implement file separation, this problem and compiler related.
Citing <<c++primer (fourth edition) >> Views: 1) Standard C + + defines two models for compiled template code: the "include" model and the "compile separately" model. 2) All compilers support the "include" model, and some compilers support the "compile separately" model.
Issue: (Post in: http://topic.csdn.net/u/20101215/15/f4f270f2-f0f9-4c5f-8765-1bfde2aeebbf.html)
The first method: Press the "include" model in C++primer to define the last line phrase in the header file of the template class: #include "Template_compile.cpp"
In the class template header file Template_compile.h:
[CPP]View Plaincopyprint?
- template<class t>
- Class Base
- {
- Public
- Base () {};
- ~base () {};
- T Add_base (t x,t y);
- };
- #include "Template_compile.cpp"
In the implementation file Template_compile.cpp of the class template:
[CPP]View Plaincopyprint?
- template<class t>
- T Base<t>::add_base (t x,t y)
- {
- return x+y;
- }
In the test file use_template.cpp that uses the template:
[CPP]View Plaincopyprint?
- #include <iostream>
- #include "template_compile.h"
- Using namespace std;
- void Main ()
- {
- base<int> bobj;
- Cout<<bobj.add_base (2,3) <<endl;
- }
This method cannot be compiled, "template_compile.cpp" file cannot "see" "template_compile.h" file.
However: If I put the code in the implementation file of the class template in the header file of the class template, comment out: #include "template_compile.cpp", compile and run without any errors. In theory, "put the code in the implementation file of the class template in the header file of the class template" and "the last line phrase in the header file that defines the template class: #include" Template_compile.cpp "" is consistent, but the compiler does not pass.
The experiment proves that VC9.0 does not support the "inclusion" model as described in C++primer.
The second method: Bruceteen proposed: Use define
In the class template header file Template_compile.h:
[CPP]View Plaincopyprint?
- template<class t>
- Class Base
- {
- Public
- Base () {};
- ~base () {};
- T Add_base (t x,t y);
- };
- #define Fuck
- #include "Template_compile.cpp"
In the implementation file Template_compile.cpp of the class template:
[C-sharp]View Plaincopyprint?
- #ifdef Fuck
- template<class t>
- T base<t>::add_base (t x,t y)
- {
- return x+y;
- }
- #endif
The test file does not change.
The experiment proves that this method can separate the class template header file and the implementation file in VC9.0.
Method Three:
In the class template header file Template_compile.h:
[CPP]View Plaincopyprint?
- template<class t>
- Class Base
- {
- Public
- Base () {};
- ~base () {};
- T Add_base (t x,t y);
- };
In the implementation file Template_compile.cpp of the class template:
[C-sharp]View Plaincopyprint?
- #include "template_compile.h"
- template<class t>
- T base<t>::add_base (t x,t y)
- {
- return x+y;
- }
In the test file use_template.cpp using the template: Use # include "Template_compile.cpp"
[C-sharp]View Plaincopyprint?
- #include <iostream>
- #include "Template_compile.cpp"
- Using namespace std;
- void Main ()
- {
- base<int> bobj;
- Cout<<bobj.add_base (2,3) <<endl;
- }
Experimental results show that this method can realize the separation of the class template header file and the implementation file in VC9.0.
In addition, the experiment proves that VC9.0 does not support the "compile separately" model.
Implementation of C + + template class header file and implementation of file separation method