Trouble creating a formatted assert wrapper

Timon Gehr timon.gehr at gmx.ch
Sun Sep 2 13:30:56 PDT 2012


On 09/02/2012 07:14 PM, Peter Alexander wrote:
> 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?

I don't think there is a deep reason why IFTI shouldn't succeed in this 
case.


More information about the Digitalmars-d mailing list