Trouble creating a formatted assert wrapper

Peter Alexander peter.alexander.au at gmail.com
Sun Sep 2 10:14:07 PDT 2012


One thing that's always bothered me is that I have to use 
std.string.format to produce useful assert messages:

assert(x == 1, format("x doesn't equal 1, it is %d", x));


Of course, I tried to solve this by producing my own assert-like 
function wrapper:

void myAssert(Args...)(lazy bool condition, lazy Args args)
{
     assert(condition, format(args));
}


That's all good, but the problem now is that I get the wrong file 
and line in the assert message, so I tried to use the default arg 
__FILE__ and __LINE__ trick:

void myAssert(Args...)(lazy bool condition, lazy Args args, int 
line = __LINE__, string file = __FILE__)
{
     if (!condition)
     {
         writeln("Assertion failed @ %s:%d", file, line);
         writefln(args);
         exit(1);
     }
}


But I can't have default arguments because of the variadic 
arguments!

Can anyone think of a way around this?


More information about the Digitalmars-d mailing list