Hey,
I am trying to wrap an unmanaged library in managed c++ so that I can use
this library in other .NET languages, such as C#. I've been successful for
the most part this far, but I'm having a hard time figuring out how to wrap
template classes. I'm getting an C3231 compile error when I try to use the
generic type as a template type.
Here's an example from the MSDN C3231 compile error site:
template <class T> class A;
generic <class T>
ref class C {
void f() {
A<T> a; // C3231
}
};
Does anyone know how to work around this?
Best regards,
Rune Vistnes
Dick Swager - 15 May 2006 03:17 GMT
Me too!
I found this article:
http://www.codeproject.com/managedcpp/cppcligenerics.asp
It indicates that generics can be used for this very purpose. And it demos
the use of typeid:
generic<typename T> static INumericalOperations<T>^ GetNumericalAdapter()
{
Type^ typ = T::typeid;
if( typ == int::typeid)
{
return dynamic_cast<INumericalOperations<T>^>(gcnew
IntNumericalAdapter());
}
if( typ == float::typeid)
{
return dynamic_cast<INumericalOperations<T>^>(gcnew
FloatNumericalAdapter());
}
return nullptr;
}
But it seems there should be a better way than having a bunch of if
statements for each type that is likely to be templatized. Basically we
need a generic T to template T conversion routine.
Dick
> Hey,
>
[quoted text clipped - 20 lines]
> Best regards,
> Rune Vistnes
Carl Daniel [VC++ MVP] - 15 May 2006 05:46 GMT
> Hey,
>
[quoted text clipped - 16 lines]
>
> Does anyone know how to work around this?
In general, it's simply not possible: Generics are a runtime mechanism
implemented by the CLR. Templates are a compile time mechanism implemented
by the C++ compiler. There's simply no way the CLR can instantiate a
template based on the generic type parameter(s) - only the C++ compiler can
do that.
-cd