Generics And Arithmetic

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages

  • Re: Generics And Arithmetic
    ... This usage is common in C++ templates. ... C++ templates are like macros and C# generics are like classes ... The compiler is correct, + cannot be applied to operands of those type, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java vs C++, A Newbies perspective.
    ... That's what Java Generics do. ... is to write templates for code, which are expanded to different code ... Every pure type constraint on code using C++ templates is ...
    (comp.lang.java.programmer)
  • Re: Performance bei Vergleichoperationen
    ... C++ Templates eben mit den Generics nicht mithalten können, ... Templates sind IMHO bei Definiton das mächtigere Tool, ... zum Beispiel Laufzeit-Fehler ergeben - was C# in diesen Szenarien ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: C# generic containers from a "C++ perspective"
    ... templates can solve but generics do not. ... The single-threaded symbolic optimizer from FFTW would currently be ... The error reporting from generics is much better than templates. ... Or do you want to cast the ints to floats? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# generic containers from a "C++ perspective"
    ... My point is that .NET languages ... already have better ways to solve that problem, independently of generics. ... The error reporting from generics is much better than templates. ... even wrote dozens of painstakingly hand-optimized routines and chose ...
    (microsoft.public.dotnet.languages.csharp)