Guide
questions
模板类必须在header中实现,而不能在cpp中实现,否则作为dll调用进行链接的时候回出错。
common solutions(Recommend)
implement template functions in header.
ThreadPool.h
1 | class SHARED_EXPORT ThreadPool { |
Seperate from headers
solutions 1
A common solution to this is to write the template declaration in a header file, then implement the class in an implementation file (for example .tpp), and include this implementation file at the end of the header.
Foo.h
1 | template <typename T> |
Foo.cpp
1 | template <typename T> |
solutions 2
Another solution is to keep the implementation separated, and explicitly instantiate all the template instances you’ll need:
Foo.h
1 | // no implementation |
Foo.cpp
1 |
|
Reference
- why-can-templates-only-be-implemented-in-the-header-file
- separate-c-template-headers-h-and
- Cpp_template_definitions_in_a_cpp_file_instead_of_header
History
- 20191012: created.