Treat a normal function as variadic?

Don Clugston dac at nospam.com.au
Wed May 30 07:35:02 PDT 2007


Robin Allen wrote:
> void f0()    {}
> void f1(...) {}
> void main()
> {
>     auto pf0 = &f0;
>     auto pf1 = &f1;
> 
>     writefln(typeid(typeof(pf0)));
>     writefln(typeid(typeof(pf1)));
> }
> 
> 
> The above program shows pf0 and pf1 to have the same type, 'void()*'. 
> (Which, incidentally, isn't a type. Shouldn't it be 'void(*)()'?)

Should be 'void function()' and 'void function(...)'
> 
> The thing is, I can call pf1(64), but not pf0(64) (wrong number of 
> arguments), even though pf0 and pf1 should behave identically, having 
> the same type.

They don't have the same type. typeid is telling lies.
Try writefln(pf0.mangleof, pf1.mangleof); and see if the two types are 
really the same.

> Basically, I need a way to treat a non-variadic function as if it were 
> variadic. I'm asking because I'm writing a scripting language, and I 
> want to make normal D functions callable from the script, where the 
> number of arguments given isn't known at compile time.

Not an easy problem.


More information about the Digitalmars-d-learn mailing list