generic class with numeric type?

H

Hyun-jik Bae

Is that not allowed to assume generic type numeric type? As far as I've
tried, I got an error with the following code:

public class AAA<T>
{
public int Foo(T a)
{
return a; // error: Cannot implicitly convert type 'T' to 'int'
}
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

where my intention is to use AAA<> on numeric value only, which is
acceptable in C++.

How can I resolve that compilation error? Please reply.
Thanks in advance.

Hyun-jik Bae
 
C

Carl Daniel [VC++ MVP]

Hyun-jik Bae said:
Is that not allowed to assume generic type numeric type? As far as
I've tried, I got an error with the following code:

public class AAA<T>
{
public int Foo(T a)
{
return a; // error: Cannot implicitly convert type 'T' to
'int' }
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

where my intention is to use AAA<> on numeric value only, which is
acceptable in C++.

How can I resolve that compilation error? Please reply.

Since .NET generics are fully compiled and represented in the IL, in order
for that IL to be verifiable (provably safe), there are limitations on what
..NET generics can do - they are severely limited compared to C++ templates
(and also have significant advantages compared to C++ templates in some
cases).

To use a type as a generic type parameter, when you write the generic class,
you specify constraints on the generic type parameters. These constraints
actually increase what you can do with the type. There are only two types
of constraints: a constructor contraint, written as new(), and an interface
constraint.

Unfortunately, there's no "IConvertsToInt" interface defined by the .NET
framework, and defining such an interface yourself would be unfulfilling at
best.

Your best bet in the example you supplied is probably to use the Convert
class

public class AAA<T>
{
public int Foo(T a)
{
return Convert.ToInt32(a);
}
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

Unfortunately, this will result in 'a' being boxed onto the managed heap,
since Convert.ToIn32 takes a parameter of type System.Object. But it will
work.

Doing arithmetic - even simple comparisions - with generic parameters is
hard. There are a couple of artibles on code project that explain the
limitations and what you can do to work around them.

Here's one of them - there's at least one other good one, but I couldn't
find it with a quick search.

http://www.codeproject.com/csharp/genericnumerics.asp

-cd
 
M

Marc

Hyun-jik Bae said:
Is that not allowed to assume generic type numeric type? As far as I've
tried, I got an error with the following code:

public class AAA<T>
{
public int Foo(T a)
{
return a; // error: Cannot implicitly convert type 'T' to 'int'
}
}

public class BBB
{
public int Goo()
{
int x = 3;
return AAA<int>.Foo(x);
}
}

where my intention is to use AAA<> on numeric value only, which is
acceptable in C++.

How can I resolve that compilation error? Please reply.
Thanks in advance.

Hyun-jik Bae

you can specify what type the generic type should be.

public class AAA<T>
where T: int
{
public int Foo(T a)
{
return Convert.ToInt32(a);
}
}

have a look to "where T ....". then you can do something like

public T Foo(T a)
{
return a;
}

or

public int Foo(T a)
{
return (int)a;
}

depends on what the method in praxis should do.
hope this helps.

cheers,
marc
 
J

Joanna Carter [TeamB]

"Marc" <[email protected]> a écrit dans le message de (e-mail address removed)...

| you can specify what type the generic type should be.
|
| public class AAA<T>
| where T: int
| {
| public int Foo(T a)
| {
| return Convert.ToInt32(a);
| }
| }

This is not possible, you really need to check suggestions before posting
answers :)

Joanna
 
M

Marc

Joanna said:
This is not possible, you really need to check suggestions before posting
answers :)

yes, you are right.
just wanna point to the "where" keyword for generics where one can
limit the types that can be used for a generic. this could help here
but it depends on the concrete implementation of the methods and their
results. i mean, does it always need to be an int or just the same as
the generic type?

cheers,
marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top