I have very limited knowledge of C#. My goal is to provide a C++ dll API to my C# co-worker. The dll has to be in C++ for legacy reasons.
Question - Can a C++ template function (shown below from VS) be marshaled in C#?
class __declspec(dllexport) Foo
{
public:
template <typename T> T* getFoo(T* fooData){return fooData;};
};
If not, any suggestions? Should each type that is passed into the template function have their own function so C# can marshal it?
Question - Can a C++ template function (shown below from VS) be marshaled in C#?
No. There is no compatible binary interface from C# to C++. You can only call exported C symbols from C#.
Theoretically, you could explicitly instantiate the templates in the C++ DLL, which will cause them to get external linkage and entries in the export symbol table. But name mangling will make the functions unusable for all practical purposes. The best way is therefore to have an intermediate C-compatible layer which calls the underlying C++ functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With