When writing CLI code to interface between C++ and C#, you often have to convert certain types in function calls so that they can be used in C# or C++. For example, std::string obviously doesn't exist in C#, so you have to convert it to String^ and vice versa.
Now I want to wrap a C++ class that exposes a templated function into C# via an interface that should expose a similar generic method. For this I wrote a CLI ref class that implements the C# interface and holds a pointer to an instance of the C++ class:
// C#
public interface ISomeClass {
void SomeMethod<T>(T param);
}
//CLI
public ref class : ISomeClass {
public:
generic<typename T>
virtual void SomeMethod(T param) {
_nativePtr->NativeMethod<T>(param); // :(
}
// Implementation details...
};
// C++
class SomeNativeClass {
public:
template<typename T>
void NativeMethod(T&& nativeParam) { /*bla*/ }
};
In my specific case (using Visual Studio 2013), this makes the compiler crash. Even though I don't think it is supposed to crash, I can guess that this is not supposed to work, after all I can't pass C# types directly into a C++ function. What I want is some mechanism through which I can convert a generic type 'T' into a C++ template type 'U'. I tried using template metaprogramming, however this does not work with C# generics in CLI, since generic classes can't be specialized.
Is there any way to make this work?