An imperfect solution, explicit instantiation. For example
templ.h in
template <typename T>
T max (T const& lhs, T const& rhs);
templ.cpp
#include "templ.h"
template <typename T>
T max (T const& lhs, T const& rhs)
{
return lhs > rhs ? lhs : rhs;
}
int max (int const& lhs, int const& rhs);
double max (double const& lhs, double const& rhs);
string max (string const& lhs, string const& rhs);
//...
In this way, the template in cpp will not fail in the linking stage after it is explicitly instantiated. The advantage is that the cpp implementation will not be exposed to customers, and only header files and library files are required. But the limitation is that the types supported by the template are limited, and new types added by users will not be supported.
The C++98 standard has the export keyword to separate template declaration and implementation. That is: templ.h
export template <typename T>
T max (T const& lhs, T const& rhs);
templ.cpp
#include "templ.h"
template <typename T>
T max (T const& lhs, T const& rhs)
{
return lhs > rhs ? lhs : rhs;
}
But few compilers implement this function, and export no longer has this function in the C++11 standard.
As for the template implementation code, even Microsoft is open source, so there should be no perfect solution.
If there is only a template, I think it is impossible. I have never seen such a pure template encapsulated in a dll. The template is not an ordinary C++ code. . If you don’t want to expose the implementation of your algorithm, then the only way is to make it into two parts: a dynamic library or a static library and a template code. That is, part of the important implementation is put into the library, and the unimportant ones are placed in the template, a bit like boost. son. .
An imperfect solution, explicit instantiation.
For example
templ.h in
templ.cpp
In this way, the template in cpp will not fail in the linking stage after it is explicitly instantiated. The advantage is that the cpp implementation will not be exposed to customers, and only header files and library files are required.
But the limitation is that the types supported by the template are limited, and new types added by users will not be supported.
The C++98 standard has the export keyword to separate template declaration and implementation.
That is:
templ.h
templ.cpp
But few compilers implement this function, and export no longer has this function in the C++11 standard.
As for the template implementation code, even Microsoft is open source, so there should be no perfect solution.
If there is only a template, I think it is impossible. I have never seen such a pure template encapsulated in a dll. The template is not an ordinary C++ code. .
If you don’t want to expose the implementation of your algorithm, then the only way is to make it into two parts: a dynamic library or a static library and a template code. That is, part of the important implementation is put into the library, and the unimportant ones are placed in the template, a bit like boost. son. .
Templates are almost impossible to do, most compilers do not support the export keyword