Generic with template as member variable

  • Thread starter Thread starter Boris
  • Start date Start date
B

Boris

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
 
Boris said:
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.
 
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
 
Boris said:
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
 
Back
Top