Strange template problem

Tom S h3r3tic at remove.mat.uni.torun.pl
Sat Jul 8 15:23:57 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:

Thanks for correcting me and sorry for being dumb and answering too 
quickly. ( I was just about to receive a pizza so I was in a hurry, he.. 
he... )

Looks like a very bizarre bug to me... the simplified case behaves 
exactly the same:


void foo(int i=20) {
}

void bar(int i) {
}

void main() {
	{
	    alias typeof(&bar) fn_t;    // comment it out and the error goes away
	}

	{
	    alias typeof(&foo) fn_t;
	    fn_t fn = &foo;
	    fn();
	}
}


That's funny... I'm can't stop thinking that I've reported a similar bug 
more than a year ago :o


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/



More information about the Digitalmars-d mailing list