How to organize the Template Files in C++
2013-07-30 17:27
603 查看
Normally you put class definitions in a header file and method definitions in a source file. Code that creates or uses objects of the class #includes the header file and obtains access to the method
code via the linker.
But the Template files can not organized like the non-template files. If you define the template class by organizing the declaration file (.h) and the definition file (.cpp) Normally. You will
get some linking errors after building the project.
But why templates don't work this way?
The reason is clear:
When the compiler encounters template method definitions, it performs syntax checking, but doesn't actually compile the templates.
When the compiler encounters an instantiation of the template, such as Ruler<int> myRuler, it writes code for an int version of the Ruler template by replacing each T in the template class definition
with int.
So, if you don't instantiate a class template for any types in your program, then the class method definitions are never compiled.
Then how to organize the Template Files? There are 3 methods listed below can be used.
1.
You can place the method definitions directly in the same header file where you define the class itself. When you #include this file in a source file where you use the template, the compiler
will have access to all the code it needs.
2.
You can place the template method definitions in a separate header file that you #include in the header file with the class definitions. Make
sure the #include for the method definitions follows the class definition; otherwise the code won't compile.
But method implementations look strange in header files.
template <typename T>
class Ruler
{
// Class definition omitted for brevity
};
#include " Ruler_Def.h"
3.
You can do by #include the method implementation source file(.cpp) in the template class definition header file, must follows the class definition.
template <typename T>
class Ruler
{
// Class definition omitted for brevity
};
#include " Ruler.cpp"
When using this technique, make sure you don't add the Ruler.cpp file to your project, because it is not supposed to be, and cannot be compiled separately; it should only be #included in a header file.
You can actually call your file with method implementations anything you want. Some programmers like to give source files that are included an .inl extension.
code via the linker.
But the Template files can not organized like the non-template files. If you define the template class by organizing the declaration file (.h) and the definition file (.cpp) Normally. You will
get some linking errors after building the project.
But why templates don't work this way?
The reason is clear:
When the compiler encounters template method definitions, it performs syntax checking, but doesn't actually compile the templates.
When the compiler encounters an instantiation of the template, such as Ruler<int> myRuler, it writes code for an int version of the Ruler template by replacing each T in the template class definition
with int.
So, if you don't instantiate a class template for any types in your program, then the class method definitions are never compiled.
Then how to organize the Template Files? There are 3 methods listed below can be used.
1.
You can place the method definitions directly in the same header file where you define the class itself. When you #include this file in a source file where you use the template, the compiler
will have access to all the code it needs.
2.
You can place the template method definitions in a separate header file that you #include in the header file with the class definitions. Make
sure the #include for the method definitions follows the class definition; otherwise the code won't compile.
But method implementations look strange in header files.
template <typename T>
class Ruler
{
// Class definition omitted for brevity
};
#include " Ruler_Def.h"
3.
You can do by #include the method implementation source file(.cpp) in the template class definition header file, must follows the class definition.
template <typename T>
class Ruler
{
// Class definition omitted for brevity
};
#include " Ruler.cpp"
When using this technique, make sure you don't add the Ruler.cpp file to your project, because it is not supposed to be, and cannot be compiled separately; it should only be #included in a header file.
You can actually call your file with method implementations anything you want. Some programmers like to give source files that are included an .inl extension.
相关文章推荐
- How to organize the Template Files in C++
- How to separate the implementation and definition for template function in c++
- How to get a type in C++ when its template argument is the argument
- How to know is the file exist in C++ - 在C++中如何判断文件是否存在
- How to implement the similar mechanism with Java final in c++?
- How to Capture the Integer-Divide-By-Zero Error in C++(提前定义信号)
- How to .gitignore all files/folder in a folder, but not the folder itself?
- How to find child controls that are located in the template of a parent control
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- How to programmatically clear the filesystem memory cache in C++ on a Linux system?
- [Django] Tell your site how to load the template files?
- How to determine the size of a class/struct in the C++?
- How to get files to show up in the Process Monitor/Report Repository
- How to change the text color of the selected property sheet tab in Visual C++
- How To Call Run() Method of the Microsoft Script Control in C++
- How to generate DLL files by GCC in the MinGW?
- How to read the environment variables in groovy email template 邮件模板中读取系统环境变量
- How to collect the full path name of the files in ASM diskgroups (文档 ID 888943.1)
- How to use the System Restore API to save and to restore system data in Visual C++
- How to get the size of file in C++ - 在C++中如何获取文件的大小