September 24, 2008
On Tue, Sep 23, 2008 at 8:23 PM, Sean Kelly <sean@invisibleduck.org> wrote:
> == Quote from Jarrett Billingsley (jarrett.billingsley@gmail.com)'s article
>> Uh, what compiler are you using?  That fails for me (1.034).
>
> 2.019

I suppose that would have been nice to know in the first place :|
September 24, 2008
>> > I'm pretty sure it's possible to partially specify a template.  Consider:
>> >
>> >    void main()
>> >    {
>> >        fn!(int)( 5 );
>> >    }
>> >
>> >    void fn(A, B)( B b ) {}
>> >
>> > This works just fine, but if I change the function declaration to:
>> >
>> >    void fn(A, B ...)( B b ) {}
>> >
>> > it fails.  Are variadic templates a special case?
>> >
>> Uh, what compiler are you using?  That fails for me (1.034).
>
> 2.019

It seems all Andrei's usages of the feature in std.algorithm have an alias parameter as the first, followed by the variadic.

So maybe this is just a code path in the compiler that hasn't been
tickled previously.
Does your code work if you make the first param an alias?

--bb
September 24, 2008
Bill Baxter wrote:
>>>> I'm pretty sure it's possible to partially specify a template.  Consider:
>>>>
>>>>    void main()
>>>>    {
>>>>        fn!(int)( 5 );
>>>>    }
>>>>
>>>>    void fn(A, B)( B b ) {}
>>>>
>>>> This works just fine, but if I change the function declaration to:
>>>>
>>>>    void fn(A, B ...)( B b ) {}
>>>>
>>>> it fails.  Are variadic templates a special case?
>>>>
>>> Uh, what compiler are you using?  That fails for me (1.034).
>> 2.019
> 
> It seems all Andrei's usages of the feature in std.algorithm have an
> alias parameter as the first, followed by the variadic.
> 
> So maybe this is just a code path in the compiler that hasn't been
> tickled previously.
> Does your code work if you make the first param an alias?

No.  Weird, huh?

    void main()
    {
        int x;
        fn!(x)( 5 );
    }

    void fn(alias a, B ...)( B b ) {}


Sean
September 25, 2008
In article <gbbtue$1uam$1@digitalmars.com>, sean@invisibleduck.org says...
>     class C
>     {
>         this() {}
>         this( int x, int y ) {}
>     }
> 
>     void main()
>     {
>         auto c = alloc!(C);
>         auto d = alloc!(C)( 1, 2 );
>     }
> 
>     T alloc(T, Params ...)( Params params )
>     {
>         return new T( params );
>     }
> 
> $ dmd test
> test.d(10): Error: expected 0 arguments, not 2

I dug this case a bit more in 2.019.  First of all, it's possible to make it work for non-zero argument count by making a single change in the template body:

    T alloc(T, Params ...)( Params params )
    {
        cast(void) params;
        return new T( params );
    }

though it stops accepting zero arguments because "tuple has no effect in expression (tuple())".  The weirdest part is when you try to access number of arguments in the tuple:

    void main()
    {
        auto d = alloc!(C)( 1, 2 );
    }

    T alloc(T, Params ...)( Params params )
    {
        cast(void) params;
        pragma( msg, Params.length.stringof );
        return new T( params );
    }

when compiling, prints:

    0u
    2u

but without the cast:

    0u
    test.d(12): Error: expected 0 arguments, not 2

So it instantiates the template once, check whether the tuple argument is explicitly used inside the template body, then instantiates it again with number of arguments depending on the previous instantiation.  Looks to me like a dirty hack in the compiler.
1 2
Next ›   Last »