Browse thread
[Caml-list] generic programming
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2002-07-04 (15:33) |
From: | John Max Skaller <skaller@o...> |
Subject: | Re: [Caml-list] Re: generic programming |
Alessandro Baretta wrote: > Templates are "nasty" citizens of the C++ world. I've had *so* many > problems tracking down bugs in code using STL templates. Basically, > the "write once, compile many" strategy of C++ yields situations where > the semantics of the algorithms coded in a template depends upon the > type parameter you pass to the template itself. Abominable. Well, it isn't that bad, but this: "the semantics of the algorithms coded in a template depends upon the type parameter you pass to the template" is entirely correct: instantiation is not functorial (it doesn't necessarily preserve the structure of the algorithm represented in the template .. alternatively, you can be 'nastier' and just say that a template algorithm doesn' *have* any semantics). FYI: as a member of the C++ committee I have fought long and hard to limit the damage by trying to insist that the type parameters of a template can only be bound to object types, and not 'const' or 'reference' types. If you allow such bindings, the semantics are wildly indeterminate. For example: template<class T> T f(T t) { return t; } f<int&>(x) // returns reference to x is utterly unlike f<int>(x) // returns copy of x and one wonders what template<class T> T &( T &t) { return t; } means when instantiated like: f<int&> // what type is int && ?? Before you can have parametric polymorphism, you need a coherent type system .. :-) -- John Max Skaller, mailto:skaller@ozemail.com.au snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia. voice:61-2-9660-0850 ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners