Re: C# generic containers from a "C++ perspective"
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Mon, 24 Mar 2008 17:01:49 -0500
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;
}
.
- Follow-Ups:
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- References:
- C# generic containers from a "C++ perspective"
- From: Giovanni Dicanio
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Arne Vajhøj
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- Re: C# generic containers from a "C++ perspective"
- From: Ben Voigt [C++ MVP]
- Re: C# generic containers from a "C++ perspective"
- From: Jon Harrop
- C# generic containers from a "C++ perspective"
- Prev by Date: Re: DataGridView...really don't understand !!!
- Next by Date: Re: How to return null from c# class constructor after a validation?
- Previous by thread: Re: C# generic containers from a "C++ perspective"
- Next by thread: Re: C# generic containers from a "C++ perspective"
- Index(es):
Relevant Pages
|