473,720 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*unmanagedMemb er;
};

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 2190

"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*unmanagedMemb er;
};

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*unmanagedMemb er;
};

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<HW ND><T>
{
};

generic <typename T>
public ref class TraitsTwo : ManagedClass<bo ol><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.nos pamwrote:
"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*unmanagedMem ber;
};

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.nos pamwrote:
>"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;
UnmanagedClas s<U*unmanagedMe mber;
};

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
instantiatio n 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::Collect ions::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::Collect ions::Generic:: IEnumerable<T>^
public ref class ManagedClass
{
T ^managedMember;
UnmanagedClass< U*unmanagedMemb er;
};

ManagedClass<Fo rm, List<Form>is ok

ManagedClass<st ring, Set<string>is ok

ManagedClass<in t, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3132
by: Graham Dumpleton | last post by:
When you have a template class with a static member variable, ie., template<typename T> class handle { public: static void* id() { return &id_; } private: static int id_; };
7
2477
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M> registrar; }; The constructor of Registrar does the registering when it is initialized.
7
12454
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this data will be initialized if it is used across many translation unit, assume that it has constructor and needs dynamic initialization? I have blindly searched for the answer but I still not thoroughly
1
1631
by: bluekite2000 | last post by:
Here is the compilable code, along w/ the error #include<iostream> #include<complex> typedef std::complex<float> ComplexSingle; using namespace std; template<typename T> class X { private: T number;
7
2145
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array class see below
12
2617
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
3
1335
by: Serge Skorokhodov (216716244) | last post by:
Hi, I just seeking advice. Some background information first because I've run into issues that seems pretty obscure to me:( Quick search through KB yields next to nothing. Simplified samples work OK as well, the problems appear in rather bulky code:( I'm trying to implement some signal processing algorithm using
3
2260
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) The parent form have a custom control, which have a public sub looking
26
3615
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even thinking. Note that since 'boxing' and 'unboxing' is involved (I think), you don't get what you...
0
8833
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5972
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2125
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.