Re: C# generic containers from a "C++ perspective"

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



Jon Harrop wrote:
Ben Voigt [C++ MVP] wrote:
C# generics cannot possibly use any feature of the type argument
that is not
expressed as an interface implementation. Some things, like
constructor parameters, cannot be expressed with interfaces, while
others, like simple arithmetics, aren't on the built-in types.
Whatever the reason, if there's no interface, C# generics can't do
it.

Sure. I'm not contesting that templates try to solve problems that
generics do not. I am saying that all of the other problems solved by
templates are better solved by other techniques, most notably
explicit code generation. Moreover, in the overlap between generics
and templates, generics are a clear win because they are so much
easier to use.

Just so you understand fully, I challenge you to create a .NET
generic for a moving average (ring buffer of N entries that returns
the mean) which can be instantiated on both double and decimal (the
only operations needed are add
and divide). For bonus points make it work with any UDT that defines
those two operations.

The C++ template is both more straightforward and more efficient.

[snip talking about OCaml]



I'm just going to pull rank here. You need to learn some decent
languages. Look at modern statically-typed functional programming
languages for a start.

Last I checked we were comparing C++ templates to .NET generics. Are you
claiming that you could insert F# everywhere you said OCaml and all your
claims remain true? Last I heard you were complaining about the miserable
performance of F#'s allocator.

You need to learn about .NET generics before you start saying they are
oh-so-much-better than C++ templates. They aren't. Maybe OCaml generic
programming is, I haven't played with that for a while, but definitely .NET
generics fall far short.

Also you completely avoided my example.

For that matter, I don't think the OCaml moving average is going to beat the
C++ template either, for performance or readability. Plus anyone trying to
do an FFT with metaprogramming is doing it for the challenge, no other
reason. A much better way is selecting between several pre-optimized
routines based on a couple of conditions like magnitude of length and
whether the length is a power of 2.

For example, how much .NET code (pick your language) would you need to
accomplish the same thing as this?

template <typename Integral>
inline bool isPowerOfTwo(Integral const n)
{
return (n & (n-1)) == 0;
}


.



Relevant Pages

  • Re: C# generic containers from a "C++ perspective"
    ... C# generics cannot possibly use any feature of the type argument that is not ... expressed as an interface implementation. ... metaprogramming (which is the other use of templates). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delphi: generics coming
    ... Be sure to mention that C++ templates are not the same as .NET generics. ... are a compile time solution, and generics are a run-time solution. ...
    (borland.public.delphi.non-technical)
  • Re: .NET 2.0 to be launched on Nov 8
    ... >> Generics is a general term, and not limited to .NET generics. ... But templates, as ...
    (borland.public.delphi.non-technical)
  • Re: CPU recommendations?
    ... each time you add a new type of motor, ... There is therefore very little benefit in keeping a clean abstract interface and separation of code layers - it adds overhead to the generated code, and it adds overhead to the development. ... generated or used for templates, ...
    (comp.arch.embedded)
  • Re: how to find the type of an object represented by an interface
    ... Simplifying, generics are strictly linked to compilation, not runtime. ... template parameter T must implement particular interface i.e. IDisposable: ... Readonly property MaximumRange() as Integer ...
    (microsoft.public.dotnet.framework.aspnet)