Partially instantiating templates?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Jan 31 10:33:38 PST 2011


On 1/31/11, Simen kjaeraas <simen.kjaras at gmail.com> wrote:
> You can only do that using aliases.
>

Yeah. I was just experimenting for the last half hour. I was hoping to
make it easier to make an alias to a nested template using the
eponymous trick. But it doesn't work at all. All I could come up with
is this trickery:

template optArg( alias pred )
{
    static class optArg2(alias fn)
    {
        static auto optArg3( Range )( Range r )
        {
            alias binaryFun!pred predicate;
            alias unaryFun!fn func;

            auto result = tuple( r.front, func( r.front ) );
            foreach ( e; r )
            {
                auto tmp = func( e );
                if ( predicate( e, result[1] ) )
                {
                    result = tuple( e, tmp );
                }
            }
            return result;
        }

        static auto opCall(Range)(Range r)
        {
            return optArg3!(Range)(r);
        }
    }

    alias optArg2 optArg;
}

void main( )
{
    alias optArg!("a<b").optArg!("a") foo;
    assert( foo( [5,2,1,3] ) == tuple(1,1) );
}

Of course, this won't work anymore:
    alias optArg!"a<b" minArg;
    alias minArg!"a" foo;
    assert( foo( [5,2,1,3] ) == tuple(1,1) );


More information about the Digitalmars-d-learn mailing list