__FILE__ and __LINE__ again...

Dicebot public at dicebot.lv
Tue Sep 10 09:08:22 PDT 2013


On Tuesday, 10 September 2013 at 16:00:55 UTC, Paolo Invernizzi 
wrote:
> Johannes Pfau wrote something like this, in the logger thread:
>
>>If you write code like this:
>>
>>void log(string file = __FILE__)() //A template
>>{
>>  logImpl(file);
>>}
>>void logImpl(string file){} //Not a template
>>
>>The compiler can always inline the log template. So there's no
>>template bloat as there will be effectively no instances of log.
>>Instead it will be inlined and logImpl will be called directly 
>>just as
>>if you manually called logImpl(__FILE__).
>
> I'm trying something like that, but with __LINE__ in addition 
> to put a little more pressure:
>
>     void log(string file = __FILE__, int line = __LINE__)(){ 
> logImpl(file, line); }
>     void logImpl(string file, int line){}
>
> I've then compiled a single file filled with 'log()', but I've 
> found from 'nm' that the text section is still full of 
> templated functions.
>
> So the question is: is Johannes trick supposed to work, or 
> there's something I don't understand well about template 
> expansion and inlining?
>
> Thanks, Paolo

Johannes was wrong. DMD will always emit template symbols, even 
if those are completely inlined. Basically, anything that is 
somehow referenced in D modules gets into object file.

Why won't you use default parameters in normal functions:

void log(string file = __FILE__, int line = __LINE)
{
     // ...
}

It uses caller context for token substitution, not declaration 
one.


More information about the Digitalmars-d-learn mailing list