Fri, 18 Dec 2009 08:53:33 -0500, bearophile wrote: > Yigal Chripun: >> There's a whole range of designs for this and related issues and IMO >> the C++ design is by far the worst of them all. > > My creativity is probably limited, so I think that while C++/D templates > have some well known problems, they are better than the strategies used > by Java, C#, Ada, Haskell, Object-C, Scala, and Delphi to define generic > code. They produce efficient code when you don't have a virtual machine > at run time, and allow to write STL-like algorithms. If you need less > performance and/or you accept worse algorithms/collections then I agree > there are designs simpler to use and cleaner than C++/D templates. If > you are able to design something better I'd like to know about your > ideas.
Templates are good for parameterizing algorithms and data structures. They begin to have problems when they are used extensively for meta- programming. For instance the lack of lazy evalution in the type world forces the language to either have 'static if' or you need to add indirection via dummy members. The language is basically purely functional, but it's several orders of magnitude more verbose than say Haskell. CTFE solves some of the problems, but as a result the system becomes really unorthogonal. Macros on the other hand solve the problem of clean meta-programmming but are not the best way to describe generic types. Java, C#, Scala, Haskell et al only support types as template parameters. In addition Java erases this type info on runtime so you get even worse performance than on C#/.NET.