oliviER++

Hi,
I am looking for a way to be able to use a C++/CLI template that is instanciated in another template.
Something like that:

Code Snippet


// assembly A
template < typename T > public ref struct X{};
template ref struct X<int>;

// assembly B
X<int> myObject;


I've searched the forum and the closest thing I found is this example:

Code Snippet

// assembly A
template < typename T > public ref struct X{};
template ref struct X<int>;

// assembly B
#using "a.dll"
ref struct Y: __identifier("X<int>")
{
};

(in this thread http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=867720&SiteID=1 )
but I can't get it to compile.
Also I would like to use my instanciated template from C# code.
Thanks for your help!


Re: Visual C++ Language Cross assembly C++/CLI instanciated templates

Holger Grund

I guess, I probably said it before, but in general you shouldn't directly export template instantiations. It's probably a better idea, to have templates inline in the headers to be instantiated for each consuming assembly on top of a shared interface.

Anyway, the example should work just fine if you disable the discretionary error for __identifier with a non-C++-keyword (C4xxx are warnings, this one being an error by default).

What error message do you get

-hg





Re: Visual C++ Language Cross assembly C++/CLI instanciated templates

oliviER++

Thanks for your answer!
I get the following error message :
error C4483: syntax error: expected C++ keyword
It refers to "X<int>". I've checked assembly A and it contains such a type.

I can't just include the header as I would like to consume the instanciated template from some C# code. It looks like the only thing that prevents C# from using this type is its name.
I unsuccessfully tried to find a way to rename a managed type at compile time, so I could have access to it from other assemblies.

olivier




Re: Visual C++ Language Cross assembly C++/CLI instanciated templates

Holger Grund

As I tried to say, you can ignore C4483. It is a warning (all compiler diagnostic messages starting with C4 are), which is an error by default. Errors, are issued in situations where the compiler cannot reasonably assume that continuing processing yields a useful result.

Warnings, can have several default states (off,Level 1-4,error). In the last case the warnings is promoted to an error an cause compilation to fail. C4483 is an example of such a warning. You can simply use #pragma warning or /wd to disable it.

-hg





Re: Visual C++ Language Cross assembly C++/CLI instanciated templates

oliviER++

Sorry for being so slow to understand that... So as you recommend I have disabled C4483 and got it to work!!! Thanks a lot!
Do you have any suggestion on how to use that instantiated template from other languages, such as C# To make thing easier I tried to change the type's name in the original assembly so to rename/map/alias X<int> to something more usual. But I couldn't find a way to do that.