Function pointer argument to function template
Daniel Keep
daniel.keep.lists at gmail.com
Tue May 30 23:41:02 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 +/
Wow. That's... so much nicer than mine. I'm not even going to post my
comparatively hideous solution :p
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
--
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list