Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is another expression template which seems to be even more efficient based on some tests that I've run. In particular, it avoids creating multiple functions for strings with different lengths by specializing <code>operator&lt;&lt;</code> to use a <code>char *</code> member in the resulting structure. It should also be easy to add other specializations of this form.</p> <pre><code>struct None { }; template &lt;typename First,typename Second&gt; struct Pair { First first; Second second; }; template &lt;typename List&gt; struct LogData { List list; }; template &lt;typename Begin,typename Value&gt; LogData&lt;Pair&lt;Begin,const Value &amp;&gt;&gt; operator&lt;&lt;(LogData&lt;Begin&gt; begin,const Value &amp;value) { return {{begin.list,value}}; } template &lt;typename Begin,size_t n&gt; LogData&lt;Pair&lt;Begin,const char *&gt;&gt; operator&lt;&lt;(LogData&lt;Begin&gt; begin,const char (&amp;value)[n]) { return {{begin.list,value}}; } inline void printList(std::ostream &amp;os,None) { } template &lt;typename Begin,typename Last&gt; void printList(std::ostream &amp;os,const Pair&lt;Begin,Last&gt; &amp;data) { printList(os,data.first); os &lt;&lt; data.second; } template &lt;typename List&gt; void log(const char *file,int line,const LogData&lt;List&gt; &amp;data) { std::cout &lt;&lt; file &lt;&lt; " (" &lt;&lt; line &lt;&lt; "): "; printList(std::cout,data.list); std::cout &lt;&lt; "\n"; } #define LOG(x) (log(__FILE__,__LINE__,LogData&lt;None&gt;() &lt;&lt; x)) </code></pre> <p>With G++ 4.7.2, with -O2 optimization, this creates a very compact instruction sequence, equivalent to filling a struct with the parameters using a <code>char *</code> for string literals.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload