Function pointer argument to function template
Bruno Medeiros
brunodomedeirosATgmail at SPAM.com
Sat Jun 3 10:27:47 PDT 2006
Tom S wrote:
> Hey there :)
>
> I came up with this:
>
> ----
>
> import std.stdio;
>
> template Deref(T) {
> alias typeof(*T) Deref;
> }
>
> template RetType(T) {
> static if (is(Deref!(T) U == function)) {
> alias U RetType;
> } else static assert (false);
> }
>
> template Bar(T, U=RetType!(T)) {
> U Bar(T t) {
> writefln(typeid(U));
>
> // do something :P
> return U.init;
> }
> }
>
>
> cfloat func(int a, float b) {
> return 1.f + 0i;
> }
>
>
> void main() {
> writefln(Bar(&func));
> }
>
>
> --
> Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Just for the record, here's an alternative version, smaller, but less
flexible(because you must know the parameters of the T function, and you
have worse error messages):
template Bar(T, U = typeof(T(0,0)) ) {
U Bar(T t) {
writefln(typeid(U));
return U.init;
}
}
cfloat func(int a, float b) {
return 1.f + 0i;
}
void main() {
writefln(Bar(&func));
}
--
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list