Capturing caller's file/line number in variadic template functions

Mantis mail.mantis.88 at gmail.com
Fri Mar 16 11:38:02 PDT 2012


16.03.2012 20:35, H. S. Teoh пишет:
> [...]

> Actually, I found the solution: the compiler understands __FILE__ and 
> __LINE__ in compile-time arguments too, so this works: void 
> checkConsistency(string file=__FILE__, size_t line=__LINE__, T...)(T 
> args) { ... } It's a bit painful with the assert statement, because it 
> doesn't let you control the file/line number of the AssertError, but 
> this can be worked around by using std.string.format: 
> assert(isConsistent(a), "inconsistency found in 
> %s(%d)".format(file,line)); The AssertError will still point to 
> checkConsistency, but the error message will tell you where the caller 
> is. T 

Yes, but the code is duplicated per every instance of template, and 
template is instantiated whenever it's CT-parameters change. This isn't 
much of a problem with functions that are meant to run at compile-time 
only (except that static locals will not work properly), but for 
run-time functions that may be undesired.


More information about the Digitalmars-d-learn mailing list