template specialization for arrays

J Arrizza cppgent0 at gmail.com
Sat Oct 29 14:02:53 PDT 2011


Still no joy:

$ dmd -v
  DMD64 D Compiler v2.055


$ rm -f dtest; dmd dtest.d ; ./dtest

dtest.d(48): found ',' when expecting ')'
dtest.d(48): found 'Elem' when expecting ')'
dtest.d(48): semicolon expected following function declaration
dtest.d(48): Declaration expected, not ')'
dtest.d(51): unrecognized declaration



//I tried both "=" and "=="
void abc(Type)(Type param1)
if(is(Type == Elem[], Elem))  //this is line48
  {
  writeln("array : ", parm1);
  }


I tried a bunch of variants, the most straightforward was:

    if(is(Type == int[]))

which gave the compile error:

dtest.d(70): Error: template dtest.abc(T) if (is(T == int[])) abc(T) if
(is(T == int[])) matches more than one template declaration,
dtest.d(36):abc(T) if (is(T == int[])) and dtest.d(42):abc(T)


line 70 is the instantiation:

    int[] arr = [1, 2];
    abc(arr);  //line 70

John

On Sat, Oct 29, 2011 at 12:40 PM, Gor Gyolchanyan <
gor.f.gyolchanyan at gmail.com> wrote:

> Try this:
>
> void abc(Type)(Type param1)
>    if(is(Type = Elem[], Elem))
> {
>    writeln("array : ", parm1);
> }
>
> You can write just about any kind of complex "is" expressions there,
> including templates with partially specialized parameters and arrays
> of all sorts.
>
> On Sat, Oct 29, 2011 at 8:11 PM, J Arrizza <cppgent0 at gmail.com> wrote:
> > Thanks for the reply Jonathan, but it didn't work for me:
> >
> > void abc(T) (T[] parm1)
> > if (isDynamicArray!T)
> > {
> >   writeln("array : ", parm1);
> > }
> >
> > Nor did:
> >
> > void abc(T) (T[] parm1)
> > if (isStaticArray!T)
> > {
> >   writeln("array : ", parm1);
> > }
> >
> > Output is the same:
> >
> > simpleparm: 1
> > simpleparm: str
> > simpleparm: [1, 2]
> >
> >
> > John
> > On Sat, Oct 29, 2011 at 8:41 AM, Jonathan M Davis <jmdavisProg at gmx.com>
> > wrote:
> >>
> >> On Saturday, October 29, 2011 08:24:29 J Arrizza wrote:
> >> > I have a template that I'd like to have a specialization for arrays.
> >> > Initiall I need it to work for byte arrays, but I'd like to make it
> >> > eventually work for all arrays. The page
> >> > http://d-programming-language.org/template says to use
> >> >
> >> > template TFoo(T : T[]) { ... } // #2
> >> >
> >> >
> >> > but when I try it, it doesn't quite work:
> >> >
> >> > template abc(T)
> >> >   {
> >> >     void abc(T parm1)
> >> >       {
> >> >         writeln("simpleparm: ", parm1);
> >> >       }
> >> >   }
> >> >
> >> > template abc(T: T[])
> >> >   {
> >> >     void abc(T parm1)
> >> >       {
> >> >         writeln("array : ", parm1);
> >> >       }
> >> >   }
> >> >
> >> >
> >> > void main(string[] args)
> >> >   {
> >> >     abc(1);
> >> >     abc("str");
> >> >     int[] arr = [1, 2];
> >> >     abc(arr);
> >> >   }
> >> >
> >> >
> >> > The output is:
> >> >
> >> > simpleparm: 1
> >> > simpleparm: str
> >> > simpleparm: [1, 2]
> >> >
> >> >
> >> > Which is not what I want, it needs to be the specialized  template for
> >> > arrays. Note, this doesn't work either:
> >> >
> >> > template abc(T: T[])
> >> >   {
> >> >     void abc(T[] parm1)
> >> >       {
> >> >         writeln("array : ", parm1);
> >> >       }
> >> >   }
> >>
> >> Use std.traits.isDynamicArray in a template constraint. eg.
> >>
> >> void abc(T)(T parm1)
> >>    if(isDynamicArray!T)
> >> {
> >> }
> >>
> >> - Jonathan M Davis
> >
> >
> >
> > --
> > John
> > blog: http://arrizza.blogspot.com/
> > web: http://www.arrizza.com/
> >
>



-- 
John
blog: http://arrizza.blogspot.com/
web: http://www.arrizza.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20111029/b918e9d8/attachment.html>


More information about the Digitalmars-d mailing list