Strange template problem

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Sun Jul 9 05:14:07 PDT 2006


Kirk McDonald wrote:
> Kirk McDonald wrote:
>> Tom S wrote:
>>
>>> Kirk McDonald wrote:
>>>
>>>> I'm having some trouble with a very odd template problem. Take the 
>>>> following code:
>>>>
>>>> [test.d]
>>>> import std.stdio;
>>>> import ftype; // Daniel Keep's ftype module
>>>>
>>>> // A function with default arguments
>>>> void foo(int i=20, char[] s="Monkey") {
>>>>     writefln("foo: %s %s", i, s);
>>>> }
>>>>
>>>> // A function with the same arguments and no defaults
>>>> void bar(int i, char[] s) {
>>>>     writefln("bar: %s %s", i, s);
>>>> }
>>>>
>>>> // A function that takes a function pointer and the minimum
>>>> // number of arguments the function can be called with, and
>>>> // calls it.
>>>> void Baz(fn_t, uint MIN_ARGS)(fn_t fn) {
>>>>     static if (MIN_ARGS == 0)
>>>>         fn();
>>>
>>>
>>>
>>> MIN_ARGS is 0 for 'foo', because thats what the NumberOfArgs template 
>>> returns, using the function *alias*. the *type* doesnt tell the 
>>> compiler that it can be called with no args. thats just a func type 
>>> that takes two args. thus the error.
>>
>>
>> Then why does it work when I comment out line 28?
> 
> Actually, I think I'm going to be really pedantic and correct you:
> 
> MIN_ARGS is 0 for 'foo' because I explicitly told it to be (on line 29). 
> NumberOfArgs would evaluate to 2, because it does indeed operate on the 
> type of the function pointer, which doesn't know a thing about default 
> arguments. If lines 28 and 29 instead read:
> 

For what I see, a function pointer *does* know about default arguments. 
That doesn't change your point though.

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list