I have a C++ library (.lib) which relies heavily on templates. I’m creating a C++ wrapper to this library to expose its functions externally in a .dll. These exposed functions will in turn be used by both Java and C# libraries via JNA/JNI and PInvoke respectively.
I understand from this SO question that it is not possible to export templates.
Does anyone have good guidance on the best way to wrap C++ templates so that as much of their flexibility is retained, but can be exported?
As an example, a typical C++ function would be the following:
template <class A, class B>
inline
A
do_something(A first, A last,
B result )
{
/* implementation */
}
When people say “it is not possible to export templates”, that does actually mean “it is not possible to export templates.
Since it is not possible to export templates, you have no way to “retain as much as possible of their flexibility”.
You can export individual functions, but not templates. You can always generate a function from a function template (by instantiating it.
template <typename T> void foo()is a template, butvoid foo<int>()is a function, albeit one with some funny symbols in its name)But no, the flexibility of templates is only there because they’re templates. When you can’t export the template, you can’t export the flexibility either. You’ll have to export the specific functions and classes that you want exported.