473,770 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++/CLI generic constructor constraint

Hi

I am trying to write a generic class that instantiates the generic
type, but I can not find the correct way to give it the constructor
constraint.

For example:
In C#:

class X<T> where T:new()
{
static public T F() { return new T(); }
}

In C++/CLI:
generic<class T> where T:???? //(new(), gcnew() or what?
class X
{
public:
static T ^F() { return gcnew T(); }
};
Nov 17 '05 #1
9 4044
Alon Fliess wrote:
Hi

I am trying to write a generic class that instantiates the generic
type, but I can not find the correct way to give it the constructor
constraint.

For example:
In C#:

class X<T> where T:new()
{
static public T F() { return new T(); }
}

In C++/CLI:
generic<class T> where T:???? //(new(), gcnew() or what?
class X
{
public:
static T ^F() { return gcnew T(); }
};


IIUC, C++/CLI doesn't support authoring of generic constraints for the VC++
2005 release. It will honor generic constraints of generics authored in
another language (C#), however. As I understand it, the ECMA committee is
still working on syntax that will support generics but not collide with the
already complicated C++ type specifier syntax.

-cd
Nov 17 '05 #2
Alon,
IIUC, C++/CLI doesn't support authoring of generic constraints for the
VC++ 2005 release. It will honor generic constraints of generics authored
in another language (C#), however. As I understand it, the ECMA committee
is still working on syntax that will support generics but not collide with
the already complicated C++ type specifier syntax.


Just to complement what Carl wrote, you can still do this in C++/CLI, but
you'll need reflection to create the object inside your generic. Here's an
example:
ref class A
{
public:
A() { Console::WriteL ine("In Constructor()") ; }
};

generic <class T>
ref class B
{
public:
T CreateT() {
return (T)Activator::C reateInstance(T ::typeid);
}
};
int main()
{
B<A^>^ b = gcnew B<A^>;
A^ a = b->CreateT();
}

--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/weblog
Nov 17 '05 #3
"Tomas Restrepo \(MVP\)" <to****@mvps.or g> wrote in message news:<us******* *******@TK2MSFT NGP14.phx.gbl>. ..
Alon,
IIUC, C++/CLI doesn't support authoring of generic constraints for the
VC++ 2005 release. It will honor generic constraints of generics authored
in another language (C#), however. As I understand it, the ECMA committee
is still working on syntax that will support generics but not collide with
the already complicated C++ type specifier syntax.


Just to complement what Carl wrote, you can still do this in C++/CLI, but
you'll need reflection to create the object inside your generic. Here's an
example:
ref class A
{
public:
A() { Console::WriteL ine("In Constructor()") ; }
};

generic <class T>
ref class B
{
public:
T CreateT() {
return (T)Activator::C reateInstance(T ::typeid);
}
};
int main()
{
B<A^>^ b = gcnew B<A^>;
A^ a = b->CreateT();
}


Hi

This is funny, I wanted to compare a C++ based factory pattern with
the Activator mechanism, to find out the performance differences and
other pros/cons (type safety, etc.) And I get an answer that I can use
the activator....
I think that I will use a template to create the Factory, however
other languages will have to implement their concrete creator
(something that generic would have solved)

Alon.
Nov 17 '05 #4
Hi Alon,
This is funny, I wanted to compare a C++ based factory pattern with
the Activator mechanism, to find out the performance differences and
other pros/cons (type safety, etc.) And I get an answer that I can use
the activator....


Well, the above *is* type safe, though it does use reflection which can
potentially be a little bit slower... however, that makes very little
difference in most cases, unless you're created a very large number of
objects in tight loops.
--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #5
"Tomas Restrepo \(MVP\)" <to****@mvps.or g> wrote in message news:<#k******* *******@TK2MSFT NGP09.phx.gbl>. ..
Hi Alon,
This is funny, I wanted to compare a C++ based factory pattern with
the Activator mechanism, to find out the performance differences and
other pros/cons (type safety, etc.) And I get an answer that I can use
the activator....


Well, the above *is* type safe, though it does use reflection which can
potentially be a little bit slower... however, that makes very little
difference in most cases, unless you're created a very large number of
objects in tight loops.


I agree with you about the type safeness, and the small performance
hit, but C++/CLI should be the strongest .Net language...

Generic constraint is a feature that C# does better (for now). I have
read that C++/CLI will support interface based constraint, and in the
ECMA draft there is a question about ctor constraint, so I'm not so
sure that they will not come up with some sort of constraint syntax
until the release.

Alon.
Nov 17 '05 #6
Alon,
I agree with you about the type safeness, and the small performance
hit, but C++/CLI should be the strongest .Net language...

Generic constraint is a feature that C# does better (for now). I have
read that C++/CLI will support interface based constraint, and in the
ECMA draft there is a question about ctor constraint, so I'm not so
sure that they will not come up with some sort of constraint syntax
until the release.


It *does* support interface based constraints, just not the constructor
constraint. Here's an example:

#using <mscorlib.dll >

using namespace System;
using namespace System::Collect ions::Generic;
generic <typename T, typename V>
where T : IEnumerable<V>
ref class IWillEnumerateY ou
{
public:
void DoIt(T t)
{
for each ( V v in t )
{
Console::WriteL ine(v);
}
}
};

int main()
{
List<String^>^ stringList = gcnew List<String^>() ;
stringList->Add("String 1");
stringList->Add("String 2");
stringList->Add("String 3");
IWillEnumerateY ou<List<String^ >^, String^>^ en
= gcnew IWillEnumerateY ou<List<String^ >^, String^>();

en->DoIt(stringLis t);

}
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/
Nov 17 '05 #7
For anyone interested, this is the latest draft:
http://www.plumhall.com/C++-CLI%20draft%201.10.pdf
I think it is high time in msdn site, the outdated draft to be replaced with this.

Nov 17 '05 #8
Ioannis Vranos <iv*@remove.thi s.grad.com> wrote in message news:<#w******* *******@TK2MSFT NGP09.phx.gbl>. ..
For anyone interested, this is the latest draft:
http://www.plumhall.com/C++-CLI%20draft%201.10.pdf
I think it is high time in msdn site, the outdated draft to be replaced with this.


Thank you very much.

I will ask someone at Microsoft to update the MSDN link.

Alon.
Nov 17 '05 #9
Alon Fliess wrote:
Thank you very much.

I will ask someone at Microsoft to update the MSDN link.

To make things easier, you may continue finding the latest drafts in here:
http://www.plumhall.com/ecma/index.html
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2719
by: Dmitry Martynov | last post by:
What if I have some special constructors and do not have a default constructor. It seems to be very useful when I do not want to forget to initialize some fields properly. As I have understood classes with no default constructor can not be arguments of generic types. Why I can not specify "new ( smth. )" as a constraint of a generic parameter? Or may be the document http://www.msdn.microsoft.com/vcsharp/language misses this case?
4
2047
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C# generic use constraint by type,complier is relaxed, but it is very limited to programmer.Is there a way to get merits of both? Maybe the following way can achieve this purpose : //First add a keyword "constrant" to modify class or struct just...
5
4580
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
6
4646
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used unless it is nullable. Why doesn't this work? public class Metadata<T> where T: System.int line from the docs
9
12849
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The natural (but illegal) notation would be something like class A<QueueClasswhere QueueClass :...
3
1743
by: BombDrop | last post by:
I'm working on a system that has 4 different types of client they have mostly the same attributes so I decided to create an interface IClient for them to implement and then just add the differing attributes as needed to each client class. Now I get the Client information from a DB and Load them into a Generic dictionary
7
1560
by: WT | last post by:
Hello, I am building a generic class that needs to instanciate an object of its template class, something like public class MyGeneric<T> { List<TmyList = new List<T>(); void addToLst(List l) {
2
3892
by: gilbert | last post by:
Hello. I am trying to use c# generic to define a class. class MyClass<Twhere T: new(){ } In this definition, MyClass can create T objects with a default constructor. Is there any way to set the constraint such that MyClass
9
3154
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, public class SomeList<T> { public SomeList(SomeList<TthisSomeList)
0
9425
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
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
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...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
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
2816
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.