Accessing __FILE__ and __LINE__ of caller in combination with varargs?

WebFreak001 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 15 13:52:42 PDT 2016


On Friday, 15 April 2016 at 20:43:08 UTC, pineapple wrote:
> I've written a very handy assertf method whose signature looks 
> like this:
>
> void assertf(Args...)(lazy bool condition, in string message, 
> Args args)
>
> But I'd also like to access the caller's file and line to use 
> them in constructing a thrown AssertError, and I'm stumbling on 
> how I can get that information where it needs to go. Help?

void assertf(string file = __FILE__, size_t line = __LINE__, 
Args...)(lazy bool condition, in string message, Args args) {
     debug {
         if (!condition) {
             writeln(format(message, args) ~ format(" in %s:%s", 
file, line));
             throw new AssertError();
         }
     }
}

---

didn't test it but it should work (replace the content of the 
function with what you actually need, this is just some 
placeholder)


More information about the Digitalmars-d-learn mailing list