Any way to mix C++ templates and C#?  
Author Message

PostPosted: Visual C# Language, Any way to mix C++ templates and C#? Top

I've been trying to write a small library that depends on C++ templates (C# generics are just too restrictive for my purposes), but I'd like to be able to access and use the final library from C#.

I haven't been able to dig up much information about this, but my observations so far are that:
1: If I just define a class template in a managed C++ assembly, it's inaccessible from another assembly (This makes perfect sense given how and when C++ templates are instantiated. I'm actually surprised this is possible between two managed C++ assemblies)

2: If I specialize the class template, the resulting type is not accessible from a C# assembly either. That is, I do something like this in my C++ assembly:

template <typename T>
class TClass {...};


This is again not *too* surprising. (Would have been nice, and I can't see any major technical problems, but I suppose you could argue that this template specialization should only be instantiated if it's actually used, which leads to the final, and more surprising point:

3: Even if I instantiate my class template, it's inaccessible. This is the part that's puzzling me a bit:

That is, if I take the above code, and then further down define a non-templated class like this:

ref class MyClass {
TClass<int> myTemplatedClass;
void foo();

then the myTemplatedClass member should act just like a regular class. It's no longer a template, it doesn't depend on any template parameters or anything and can be fully instantiated at compile-time. So I'd expect this class member to be visible from my C# assembly. But it isn't.
I can see MyClass, and I can see foo(), but myTemplatedClass Nope. If I try to access it I'm told that the type "is not supported by the language". But why not It's been instantiated with all neccesary type arguments, it's been the template types are known at compile-time, it's been compiled and it should act like any regular class... Shouldn't it

Is there any way around this Is there *any* way in which I can expose C++ template instantiations (or class members using an instantiated template as their type) to a C# assembly

And is there some very good reason for this limitation
The only one I can think of is the naming. (Obviously C# won't like it if the type is called TClass<int> because hey, that sounds like a generic class, which isn't the same thing. But if nothing else, those long messy names the C++ linker uses could be exposed... or perhaps refer to it as TClass%int% or something. Sure, it's not pretty, but I can't see how it'd be in any way harmful.

(Yeah, I know this question is technically about C++, but since it's C# that won't recognize template instantiations, I figured it'd be more relevant here. The above sample works fine between two C++ assemblies)

Visual C#8  
Peter Ritchie

PostPosted: Visual C# Language, Any way to mix C++ templates and C#? Top

You can't directly access C++ templates from .NET, you have to use generics or write a wrapper to get at the template...

Figo Fei - MSFT

PostPosted: Visual C# Language, Any way to mix C++ templates and C#? Top


As Peter said, you can use C# generics.

Here is some info about it: url=/library/en-us/dnaa/html/getstart_vcsharp.asp



PostPosted: Visual C# Language, Any way to mix C++ templates and C#? Top

No I can't. As I said in the original post, generics are too restrictive for what I'm trying to do. (I already know C# and use generics extensively. Thanks for the links though ;))

But guess what I'm after is impossible then... Thanks for clearing that up though :)