mastermemorex

Hi!

I am trying to export some libraries that are written in C++ to a DLL with the /clr option so it can be used in C#. I have some troubles to fully understand the how to export templates and generics to a CLR library. I have been searching the web for days for a good answer and I only find blogs that explain the goods and bads between generics and templates. And that is not the answer I am looking for.

For example, I have this code in C++, compiled in a DLL and exported to a C# project.

Code Snippet

namespace MyDLL

{

generic<class T>

public ref class GenericRefClass

{

public:

T value;

}

template<class T>

public ref class TemplateRefClass

{

public:

T value;

}

generic<class T>

public value class GenericValueClass

{

public:

T value;

}

template<class T>

public value class TemplateValueClass

{

public:

T value;

}

}

When I import MyDLL to a C# project I only see GenericRefClass and GenericValueClass. Is like the templates are not implemented in the library.

in ECMA 334 pag. 74 says

"C# generics will immediately be familiar to users of generics in Eiffel or Ada, or to users of templates in C++."

Written like that is like generics in C# are the same as templates in C++. But it should be wrong.

I know that templates are resolved during compilation and generics during run time. So, does that means that there is no way that templates could be interchangable through a dll Or is there another way

Because there is a HUGE problem. How can I reuse the boost libraries http://www.boost.org/ that are mostly templates in a C# project

Thanks in advance.



Re: Visual C++ General How to export C++ templates to CLR library to be used in C#?

Bruno van Dooren

Nope. there is no way to make C++ templates available to C#.

In fact, it is not even possible to make templates available to other native C++ projects via a DLL. Templates don't exist as such. Only specializations exist and can be exported.

To conclude: you cannot use boost templates in C#.





Re: Visual C++ General How to export C++ templates to CLR library to be used in C#?

mastermemorex

Ugh! That was precisely my point. But you don't fully answer my question.

Anyway. Thanks for the quick and clear answer! It is something that I always appreciate.

How can I explain ...
I suppose that when you compile with the /clr option it creates a library with a CLR code, isn't it. In some way C# and C++, all work using the same standar language, aren't they

Well, Is There any way that I can make a CLR source from C++ (in form of a dll or whatever) that can be included in a C# project, so it can resolve the templates during the compilation.

I am not talking about linking a library, but including a CLR source in another project. (Maybe I am totally crazy)

Or the CLR standar just do not support templates and it is just useless trying to use C# with most of the standard libraries. What it will be very unfortunate.

And by the way. How IList<T>, that it suppose to be a higly efficient data manager, that comes in the STL, is implemented anyway

Well, that should deserves another thread...


I am really sorry about my ignorance.







Re: Visual C++ General How to export C++ templates to CLR library to be used in C#?

Bruno van Dooren

No problem. But no, there is no way whatsoever to resolve templates in C#.

C++ templates do not exist. You cannot compile template code.

But as soon as you specialize the code, the C++ compiler performs a copy / paste action to replace T with your template parameter and compiles the lot as if you made a specific class. Ok the reality is more complex than this, but conceptually this is what happens.

The end result is a class that is identical to any other C++ class.

If the template parameters are invalid for the class you get compiler errors.

With Generics, the generic class really exists as a generic thing that lives in a DLL.

The CLR standard does not support templates, only generics. The reason is what I mentioned before: only Generics can exist in DLLs.