Generics And Arithmetic

G

Gary Brown

Hi,

When I do the following

public class IAmAGeneric<T>
{
...
public T AMember (T a, T b) { return a + b; };
}

C# complains that

operator '+' cannot be applied to operands of type 'T' and 'T'

I haven't found any documentation that says this won't work but
I haven't found any that says it will. Nor an example. Is this a
limitation of C# generics? Is there a workaround?

This usage is common in C++ templates. Is this limitation because
C++ templates are like macros and C# generics are like classes
(according to the C# head honcho).

Thanks,
Gary
 
T

Torben K. Jensen

Gary said:
When I do the following

public class IAmAGeneric<T>
{
...
public T AMember (T a, T b) { return a + b; };
}

C# complains that

operator '+' cannot be applied to operands of type 'T' and 'T'

If you try and instantiate your class above as IAmAGeneric<int>, I bet it'll work. However, if you use types that don't have the '+' operator overloaded, then of course it won't.
 
C

Carl Daniel [VC++ MVP]

Gary said:
Hi,

When I do the following

public class IAmAGeneric<T>
{
...
public T AMember (T a, T b) { return a + b; };
}

C# complains that

operator '+' cannot be applied to operands of type 'T' and 'T'

I haven't found any documentation that says this won't work but
I haven't found any that says it will. Nor an example. Is this a
limitation of C# generics? Is there a workaround?

Within a generic method, you can only call methods that are known to be
valid for the generic type(s). With a plain generic, like you've shown
here, that means you can only call the members of System.Object, since those
are the only members known to be valid for any T.

So, you ask, how do you do something more interesting? With constraints:

public class IAmAGeneric<T> where T : ISomeInterface
{
}

Sadly, there's no interface that includes overloaded operator +, so there's
no way you can actually write a+b where a & b are typed by a generic type
parameter.

See here:

http://msdn2.microsoft.com/en-us/library/d5x73970.aspx

for more details.
This usage is common in C++ templates. Is this limitation because
C++ templates are like macros and C# generics are like classes
(according to the C# head honcho).

..NET generics are not C++ templates. They have their strengths and their
weaknesses compared to C++ templates. This is one of their big weaknesses
(and is a consequence of one of their big strengths).

-cd
 
T

Torben K. Jensen

I stand corrected (now that I've tried your code).

Here's a possible solution:


public abstract class PlusOp {

public static PlusOp operator +(PlusOp first, PlusOp second) {
return first.AddTo(second);
}

public abstract PlusOp AddTo(PlusOp value);
}

public class IAmAGeneric<T> where T : PlusOp {

public T AMember(T a, T b) {
return (T)(a + b);
}
}


Hope that helps.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Gary said:
When I do the following

public class IAmAGeneric<T>
{
...
public T AMember (T a, T b) { return a + b; };
}

C# complains that

operator '+' cannot be applied to operands of type 'T' and 'T'

I haven't found any documentation that says this won't work but
I haven't found any that says it will. Nor an example. Is this a
limitation of C# generics? Is there a workaround?

This usage is common in C++ templates. Is this limitation because
C++ templates are like macros and C# generics are like classes
(according to the C# head honcho).

As other have already explained then the usage of T is
type checked.

The C# generics model is simply significantly
different from the C++ template model.

If you want to see some of the weird stuff people
has been doing for workarounds then check:

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

Arne
 
T

Tom Spink

Gary said:
Hi,

When I do the following

public class IAmAGeneric<T>
{
...
public T AMember (T a, T b) { return a + b; };
}

C# complains that

operator '+' cannot be applied to operands of type 'T' and 'T'

I haven't found any documentation that says this won't work but
I haven't found any that says it will. Nor an example. Is this a
limitation of C# generics? Is there a workaround?

This usage is common in C++ templates. Is this limitation because
C++ templates are like macros and C# generics are like classes
(according to the C# head honcho).

Thanks,
Gary

Hi Gary,

The compiler is correct, + cannot be applied to operands of those type,
because the + operator may not be defined for the type specified in the
generic parameter, for example, the + operator is defined for 'int' but not
for 'ListViewItem'.

So, if I had an 'IAmAGeneric<ListViewItem>' the + operator would not be
valid. The compiler knows this, and so won't compile if there's the
possibility of a type mismatch.
 
G

Gary Brown

Thanks for the responses.

Some of the articles pointed to imply this restriction is temporary
until Whidbey. Is this true? And when and what is Whidbey (other
than an island)?

Gary
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Gary Brown said:
Thanks for the responses.

Some of the articles pointed to imply this restriction is temporary
until Whidbey. Is this true? And when and what is Whidbey (other
than an island)?

Whidbey was the codename for VS 2005 , so you are using it already :)

IMO this restriction will be permanent, unless that another mechanism is
introduced.

A possible workaround though is using reflection. You may be able to call
the "+" operation using reflection.
 
M

Marc Gravell

If only for a few more core interfaces... perhaps something like:

// e.g. most numeric types, plus other structures - matrices etc
//(undefind methods per type could always throw NotSupportedException)
public interface IArithmetic<T> : IEquatable<T> {
// not IComparable<T>, as not always sequenced?
// callers could always insist IComparable<T> separately
T Add(T value);
T Subtract(T value);
T DivideBy(T value);
T MultiplyBy(T value);
// needed to support many core math functions; shame
// no static interface memebers...
T One { get;}
T Zero { get;}
}
// e.g. int, long, uint, ulong, enums (although enums don't currently
support IEquatable<themselves>)
public interface ILogical<T> : IEquatable<T> {
// not sure should be shiftable, as
// doesn't make much sense for enums etc
T Or(T value);
T And(T value);
T Not();
T Xor(T value);
// ditto static members
T None { get;}
T All { get;}
}

Then afterwards I googled it and found the following, which is alarmingly
similar (for IArithmetic<T>) - convergence?...

http://www.lambda-computing.com/publications/articles/generics/

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