Template argument types

bearophile bearophileHUGS at lycos.com
Mon Jun 11 17:04:21 PDT 2012


Do you know why D templates accept floating point values and even 
arrays of 32 bit dchars:

template Foo(dchar[] s) {
     enum size_t Foo = s.length;
}
void main() {
     pragma(msg, Foo!("hello"d.dup));
}


But they don't accept arrays of ints?


template Foo(int[] s) {
     enum size_t Foo = s.length;
}
void main() {
     enum int[] a = [1, 2, 3];
     pragma(msg, Foo!a);
}


===>
test.d(1): Error: arithmetic/string type expected for 
value-parameter, not int[]


This works, but it's not the same thing:

template Foo(alias s) if (is(typeof(s) == int[])) {
     enum size_t Foo = s.length;
}
void main() {
     enum int[] a = [1, 2, 3];
     pragma(msg, Foo!a);
}

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list