Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2006

Tip: Looking for answers? Try searching our database.

Wrapping a unmanaged template class in a managed generic class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rune Vistnes - 12 May 2006 18:10 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.