Template design with required type parameters?
Benji Smith
dlanguage at benjismith.net
Fri Nov 10 18:04:03 PST 2006
news:ej31kf$1po9$1 at digitaldaemon.com...
> Is it possible to design a template that requires, at compile time, that
> it's parameters be of a particular type?
JC wrote:
> void goDancing(T)(T o) {
> in {
> static if (!is(T : ICanDance)) // check if T is derived from ICanDance
> static assert(false, "Type must be of type 'ICanDance'.");
> }
> body {
> o.dance();
> }
He was probably thinking something along the lines of the C# construct:
class SortedMap<K, V> where K : IComparable {
// ...
}
It looks like the contract & assertion will accomplish the same thing,
though not quite as cleanly as the parametric constraint in C#.
--benji
More information about the Digitalmars-d-learn
mailing list