__FILE__ and __LINE__ again...

Paolo Invernizzi paolo.invernizzi at gmail.com
Tue Sep 10 09:00:53 PDT 2013


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


More information about the Digitalmars-d-learn mailing list