473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4023
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::WriteLine("In Constructor()"); }
};

generic <class T>
ref class B
{
public:
T CreateT() {
return (T)Activator::CreateInstance(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.org> wrote in message news:<us**************@TK2MSFTNGP14.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::WriteLine("In Constructor()"); }
};

generic <class T>
ref class B
{
public:
T CreateT() {
return (T)Activator::CreateInstance(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.org> wrote in message news:<#k**************@TK2MSFTNGP09.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::Collections::Generic;
generic <typename T, typename V>
where T : IEnumerable<V>
ref class IWillEnumerateYou
{
public:
void DoIt(T t)
{
for each ( V v in t )
{
Console::WriteLine(v);
}
}
};

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

en->DoIt(stringList);

}
--
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.this.grad.com> wrote in message news:<#w**************@TK2MSFTNGP09.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
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...
4
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#...
5
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...
6
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...
9
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...
3
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...
7
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...
2
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...
9
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, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.