468,537 Members | 2,337 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

Generic with template as member variable

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
Jun 11 '07 #1
3 1997

"Boris" <bo***@gtemail.netwrote in message
news:op***************@burk.t-com.ne.jp...
>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
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
Jun 12 '07 #2
On Tue, 12 Jun 2007 14:38:53 +0900, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
"Boris" <bo***@gtemail.netwrote in message
news:op***************@burk.t-com.ne.jp...
>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

Template instantiation is done by the C++ compiler, generic
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
[...]
Jun 12 '07 #3

"Boris" <bo***@gtemail.netwrote in message
news:op***************@burk.t-com.ne.jp...
On Tue, 12 Jun 2007 14:38:53 +0900, Ben Voigt [C++ MVP]
<rb*@nospam.nospamwrote:
>"Boris" <bo***@gtemail.netwrote in message
news:op***************@burk.t-com.ne.jp...
>>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

Template instantiation is done by the C++ compiler, generic
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?
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<Tis a template, not
generic.
>
Boris
>[...]

Jun 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Graham Dumpleton | last post: by
7 posts views Thread by Drew McCormack | last post: by
7 posts views Thread by ank | last post: by
7 posts views Thread by Tony Johansson | last post: by
12 posts views Thread by mlimber | last post: by
3 posts views Thread by Serge Skorokhodov (216716244) | last post: by
3 posts views Thread by Seth Gecko | last post: by
26 posts views Thread by raylopez99 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.