hidden passing of __FILE__ and __LINE__ into function

Solomon E via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 18 07:40:11 PDT 2017


On Tuesday, 18 April 2017 at 13:48:57 UTC, Stanislav Blinov wrote:
> On Tuesday, 18 April 2017 at 13:28:06 UTC, Solomon E wrote:
>
>> I tried to produce an example of calling a function with
>> variadic template arguments using special tokens __FILE__ and 
>> __LINE__.
>>
>> This compiles and runs, producing the output shown, using the 
>> default gdc
>> provided by Ubuntu 17.04. This appears to be a workaround for 
>> Issue 8687...
>
> There's a much more concise workaround, both in code written 
> and generated ;)
>
> import std.stdio;
>
> template func(string file = __FILE__, int line = __LINE__)
> {
>     auto func(T...)(auto ref T args)
>     {
>         writeln("called func with ", T.length, " args at ",
>                 file, ":", line);
>     }
> }
>
> void main()
> {
>     func();
>     func(1, 2, 3);
> }

Thank you for reminding me that templates can contain a 
definition of a function
of the same name, which still surprises me as convenient, not as 
a bad thing.

Unfortunately, when I tried it, I found that while that attempt 
at a workaround is
twice as concise in lines of code and symbols introduced, it is 
not acceptable.
It gets the lines reported wrong. It reports the line for where 
the template was
first instantiated for the same tuple of argument types, instead 
of exactly the line
of each call of `func`.


More information about the Digitalmars-d-learn mailing list