Template constraints in D

bearophile bearophileHUGS at lycos.com
Sat Jun 21 04:44:30 PDT 2008


Walter Bright:
> http://www.digitalmars.com/d/2.0/concepts.html

They look nice (but I may use a bigger list of examples to understand how they can be used). What can they do that can't be done with the things already present?

bool isprime(int n) { ... }
template Foo(int N) {
    static assert (isprime(N), "error");
    ...
}


template Foo(int N) {
    static if (N & 1) {
        // A code
    } else {
        // B code
}
Foo!(3)    // instantiates Foo with A code
Foo!(64)   // instantiates Foo with B code

Bye,
bearophile



More information about the Digitalmars-d mailing list