template specialization for arrays
J Arrizza
cppgent0 at gmail.com
Sun Oct 30 03:16:43 PDT 2011
>
> You should use either std.traits.isXxx systematically, or patterns
>> systematically, but not both at the same time. Personally I prefer isXxx
>> because they foster simple logic to decide what overloads should apply.
>
>
> Also, when posting, you may want to include complete short programs so
> others can try them quickly.
Andrei, I thought I had posted the entire program. Here it is again using
only traits as you recommend:
import std.stdio;
import std.traits;
void abc(T) (T parm1)
if (isNarrowString!T || (!isStaticArray!T && !isDynamicArray!T))
{
writeln("simpleparm: ", parm1);
}
void abc(T) (T parm1)
if (!isNarrowString!T && (isDynamicArray!T || isStaticArray!T) )
{
writeln("array : ", parm1);
}
void main(string[] args)
{
writeln("v4");
abc(1);
abc("str");
int[] arr = [1, 2];
abc(arr);
int[2] arr2 = [3, 4];
abc(arr2);
}
And it does work, here's the output:
simpleparm: 1
simpleparm: str
array : [1, 2]
array : [3, 4]
Note my original intent was to differentiate between arrays and non-arrays
only (lumping strings into non-array).
As for using only patterns, I can't get the compiler to disambiguate
between non-arrays and arrays:
import std.stdio;
import std.traits;
void abc(T, U = void, size_t N = 0) (T parm1) //line 3
{
writeln("simpleparm: ", parm1);
}
void abc(T: U[N], U = char, size_t N) (T parm1)
{
writeln("string : ", parm1);
}
void abc(T) (T parm1)
{
writeln("dynamic array : ", parm1);
}
void abc(T: U[N], U, size_t N) (T parm1) //line 15
{
writeln("static array : ", parm1);
}
void main(string[] args)
{
writeln("v4");
abc(1);
abc("str");
int[] arr = [1, 2];
abc(arr);
int[2] arr2 = [3, 4];
abc(arr2); //line 27
}
Here's the compiler error:
dtest.d(27): Error: template dtest.abc(T,U = void,ulong N = 0) abc(T,U =
void,ulong N = 0) matches more than one template declaration,
dtest.d(3):abc(T,U = void,ulong N = 0) and dtest.d(15):abc(T : U[N],U,ulong
N)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20111030/e25c8f0f/attachment-0001.html>
More information about the Digitalmars-d
mailing list