On Thu, 14 Mar 2013 15:57:59 -0700 "H. S. Teoh" <[email protected]> wrote: > > In fact, now that I think of it, templates in general suffer from this > problem: when you write template X(T) { ... }, pretty much anything > goes in the template body, even outright ridiculous things that won't > compile for *any* template arguments. But more commonly, the template > body may assume things about T that don't apply across all types T. > You may assume that T is mutable, or that T has a certain member, > etc.. > > What if templates were modified so that assumptions about T have to be > stated up front? We don't necessarily have to introduce typeclasses > as a separate thing, but we could have the compiler reject template > bodies that try to access internals of T that aren't stated up front. > E.g.: >
C#'s generics work that way. I always liked that. The only problem is that MS never bothered to provide a constraint for "can do arithmetic" (like they have for "can be compared", ie IComparable) so you can't do a basic thing like "a + b" generically.
