template specialization for arrays
Timon Gehr
timon.gehr at gmx.ch
Sun Oct 30 03:13:06 PDT 2011
On 10/30/2011 01:34 AM, J Arrizza wrote:
> Thank you Timon, I very much appreciate your (and others) help.
>
> I will look all this up in the online docs and the book.
>
> There is still some very odd things in how these fit together. For
> example, if I comment out the last 3 function templates, I get all
> "simpleparms", which is expected.
>
> But if I add in the fourth, I get a compilation error saying the call to
> abc(arr2) is ambiguous. So even though the first one specifies "U =
> void" and "size_t N = 0", and the fourth specifies something very
> different, there is ambiguity between them.
>
> Adding in the third resolves the ambiguity between the first and fourth.
> John
>
I cannot reproduce that behaviour. If I comment out the second and the
third, I get 3 "simpleparm"s and 1 "static array"
> On Sat, Oct 29, 2011 at 4:02 PM, Timon Gehr <timon.gehr at gmx.ch
> <mailto:timon.gehr at gmx.ch>> wrote:
>
>
> This works:
>
> void abc(T, U=void, size_t N=0) (T parm1) {
> writeln("simpleparm: ", parm1);
> }
>
> void abc(T: string) (T parm1) {
> writeln("string : ", parm1);
> }
>
> void abc(T:U[], U) (T parm1) if (isDynamicArray!T) {
> writeln("dynamic array : ", parm1);
> }
>
> void abc(T:U[N], U, size_t N) (T parm1) {
> writeln("static array : ", parm1);
>
> }
>
> void main(string[] args) {
> abc(1);
> abc("str");
> int[] arr = [1, 2];
> abc(arr);
> int[2] arr2 =[3, 4];
> abc(arr2);
> }
>
>
Whoops, just noticed that I accidentally left the if(isDynamicArray!T)
in there. That is not necessary, if the pattern matches it will always
evaluate to true.
void abc(T, U=void, size_t N=0) (T parm1) {
writeln("simpleparm: ", parm1);
}
//void abc(T: string) (T parm1) {
// writeln("string : ", parm1);
//}
//void abc(T:U[], U) (T parm1){ // no constraint necessary
// writeln("dynamic array : ", parm1);
//}
void abc(T:U[N], U, size_t N) (T parm1) {
writeln("static array : ", parm1);
}
void main(string[] args) {
abc(1); // simpleparm
abc("str"); // simpleparm
int[] arr = [1, 2];
abc(arr); // simpleparm
int[2] arr2 =[3, 4];
abc(arr2); // static array
}
What compiler version are you using?
More information about the Digitalmars-d
mailing list