473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Wrapping a unmanaged template class in a managed generic class

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
May 12 '06 #1
2 3330
Me too!

I found this article:
http://www.codeproject.com/managedcp...ligenerics.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

"Rune Vistnes" <Rune Vi*****@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
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

May 15 '06 #2
Rune Vistnes wrote:
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?


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
May 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Kantowitz | last post by:
I am trying to wrap a native-C++ DLL in managed C++, to use in a .NET project. The native code is compiled into a DLL, and I have created a .def file that exports the mangled names of the...
0
by: sklett | last post by:
I'm having a really hard time with wrapping an unmanaged class with a managed class, then calling that managed class from my C# code. I will show you the three pieces, then explain: --------...
2
by: Paul Kenny | last post by:
Hi, I am trying to expose the functionality of an unmanaged C++ class to the other languages available in the .NET Framework. I have decided to do this by wrapping the unmanaged C++ class in a...
0
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
1
by: jsroberts | last post by:
I have been desperately trying to solve a problem where I am wrapping an unmanaged dll with managed extensions for c++ and then calling the managed dll functions from C#. However, whenever I call...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
13
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to...
12
by: doug | last post by:
I understand the basics of what managed code offers and that you open yourself up to security issues if you allow unmanaged code. We already have a decent amount of VB6 code to include COM DLLs. ...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.