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++ / June 2007

Tip: Looking for answers? Try searching our database.

Generic with template as member variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Boris - 11 Jun 2007 14:23 GMT
I have a class which should like this ideally:

generic <typename T>
public ref class ManagedClass
{
    T ^managedMember;
    UnmanagedClass<U> *unmanagedMember;
};

I actually would like to specify the type U depending on the type T. As  
far as I understand there is no direct support for this in C++/CLI (I  
can't use U as a parameter for the generic for example)? Did anyone solve  
this problem before and can recommend a solution? As ManagedClass will be  
used by customers the worst solution would be to duplicate ManagedClass  
myself (thus providing two classes which do basically the same).

Boris
Ben Voigt [C++ MVP] - 12 Jun 2007 06:38 GMT
>I have a class which should like this ideally:
>
[quoted text clipped - 9 lines]
> can't use U as a parameter for the generic for example)? Did anyone solve
> this problem before and can recommend a solution? As ManagedClass will be

Template instantiation is done by the C++ compiler, generic instantiation
for value types by the JIT, and generics are not instantiated for ref types.

I think you can make a template generic:

template <typename U>
generic <typename T>
public ref class ManagedClass
{
T ^managedMember;
UnmanagedClass<U> *unmanagedMember;
};

Then you have to make sure it is instantiated inside the C++ assembly.
Since you probably also want a nice CLR-compliant name to use from C#, etc,
probably something like:

generic <typename T>
public ref class TraitsOne : ManagedClass<HWND><T>
{
};

generic <typename T>
public ref class TraitsTwo : ManagedClass<bool><T>
{
};

> used by customers the worst solution would be to duplicate ManagedClass
> myself (thus providing two classes which do basically the same).

The above lets the C++ compiler do all the work of duplication for you.
Note that I could very well be wrong on the ordering of the template and
generic arguments... I haven't had a need to do this before.

> Boris
Boris - 12 Jun 2007 13:38 GMT
>> I have a class which should like this ideally:
>>
[quoted text clipped - 14 lines]
> instantiation for value types by the JIT, and generics are not  
> instantiated for ref types.

Thanks for your reply, Ben! As it turns out I can actually make the class  
template-only (at least for my purpose as I know at compile time which  
types depend on what). I'm not so sure anymore if my question made much  
sense at all as the unmanaged type U can't really depend on the managed  
type T if U is provided at compile time but T at runtime?

Boris

> [...]
Ben Voigt [C++ MVP] - 12 Jun 2007 14:46 GMT
>>> I have a class which should like this ideally:
>>>
[quoted text clipped - 20 lines]
> sense at all as the unmanaged type U can't really depend on the managed
> type T if U is provided at compile time but T at runtime?

I missed that part.  Actually, U can depend on T only via the generic
mechanism.  For example, U could be
System::Collections::Generic::IEnumerable<T>^.... and I'm not sure whether
you there is support for generic generic in the same way as template
templates.  I don't think so, I think U would have to be explicitly stated
by the user.

What I'm talking about, is of course:

generic <typename T, typename U>
   /* questionable syntax here */ where U :
System::Collections::Generic::IEnumerable<T>^
public ref class ManagedClass
{
 T ^managedMember;
 UnmanagedClass<U> *unmanagedMember;
};

ManagedClass<Form, List<Form>> is ok

ManagedClass<string, Set<string>> is ok

ManagedClass<int, cli::array<int>> is ok

But of course, then U isn't an unmanaged type.  Wonder if you could use
generics in an unmanaged type... I think gcroot<T> is a template, not
generic.

> Boris
>
>> [...]

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.