Function pointer argument to function template
Kirk McDonald
kirklin.mcdonald at gmail.com
Wed May 31 02:17:06 PDT 2006
Daniel Keep wrote:
> May as well ask this: do you know a simple way to infer both the number
> and type of a function's arguments? I have a set of templates that can
> do this, but they're somewhat ugly.
>
> -- Daniel
>
In C++, at least (which, yes yes, is not D, but it is similar enough in
this respect), you had to do something like the following:
template Foo(Fn) {
void Foo(Fn fn) {
fn();
}
}
template Foo(Fn, A0) {
void Foo(Fn fn, A0 a0) {
fn(a0);
}
}
template Foo(Fn, A0, A1) {
void Foo(Fn fn, A0 a0, A1 a1) {
fn(a0, a1);
}
}
// Ad absurdum, for as many args as you wish to support.
Ugly, no? Try looking through the Boost::Tuple library sometime for the
ultimate real-world example of this. (It supports this for up to 10
arguments.)
What would totally nip this thing in the bud would be variadic
templates, where something like this would work:
template Foo(Fn, ...) {
void Foo(Fn fn, ...) {
fn(<pass_args_somehow>);
}
}
I think I saw a post on this newsgroup somewhere where Walter said he'd
like to see this in D... ah! Here's the quote:
Walter Bright wrote on Feb 9, 2006:
> I know it'd be cool, and I want to do it. But it's a 2.0 thing.
-Kirk McDonald
More information about the Digitalmars-d
mailing list