On Tuesday, 3 July 2012 at 03:15:02 UTC, Wouter Verhelst wrote:
So, I wanted to create a number of functions that would call
write(),
writef(), writefln(), or writeln() with whatever arguments they
were
given, but only if the user had used a 'enable debugging'
command-line
option (or some such).
What I first did was this:
module debugout;
int debuglevel;
void set_level(int level) {
debuglevel = level;
}
auto tdebug(func)(int level, ...) {
va_list va;
va_start(va, level);
if(debuglevel <= level) {
return func(va);
}
va_end(va);
}
alias tdebug!(write) wdebug;
alias tdebug!(writef) wdebugf;
(... and so on...)
but that didn't work:
debugout.d(20): Error: template instance tdebug!(write)
tdebug!(write) does not match template declaration tdebug(func)
I had a short look at
http://dlang.org/variadic-function-templates.html
which would seem to be a somewhat better way of implementing
this, but I
can't figure out how exactly I should go forward, then.
I do believe that something like the following would do what I
want:
auto wdebug(level, ...) {
va_list va;
va_start(va, level);
writefx(stdout, ???, va);
va_end(va);
}
except the va_list stuff doesn't seem to be necessary, I don't
know what
to put in place of the "???", and I'd have to write this four
times
rather than being able to template it four times.
How should I do this instead?
Try this - http://dpaste.dzfl.pl/3efd9c1b