Overloading templates

Sean Kelly sean at invisibleduck.org
Thu Sep 25 11:12:16 PDT 2008


Okay, it turns out that this works for me in D1 as well:

    template find(alias equals)
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    template find(char[] equals = "a == b")
    {
        size_t find(Buf, Pat)( Buf buf, Pat pat )
        {
            return 0;
        }
    }

    void main()
    {
        find!()( "abc", 'a' ); // compiles in D1
        find( "abc", 'a' ); // doesn't compile in D1
    }

However, it doesn't work in D2.  There, basically the only syntax I can
find that works is what's used in std.algorithm:

    size_t find(alias equals = "a == b", Buf, Pat)( Buf buf, Pat pat )
    {
        return 0;
    }

    void main()
    {
        bool equals(char a, char b) { return a == b; }

        find( "abc", 'a' );
        find!("a == b")( "abc", 'a' );
        find!(equals)( "abc", 'a' );
    }

This is really ideal, but the fact that I can't find any common syntax
for D1 and D2 is not.  D1 doesn't allow aliasing strings but does
seem to support some degree of overloading (even if doing so requires
supplying an empty template parameter list), while D2 does allow
aliasing strings but I can't get even that shoddy form of overloading
to work.

I was really hoping to find some way to allow tango.core.Array to use
both its current delegate syntax and the new string syntax, but as it
stands I don't think this is possible if the code must be portable
between D1 and D2.


Sean


More information about the Digitalmars-d-learn mailing list