By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

C++/CLI generic constructor constraint

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.