>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
>
>> [...]