Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Preface</h3> <p>Two solutions will be found in this post, one using C++03 and the other C++11.</p> <p>It's hard (ie. you'll need to write a lot of code) if you'd like a true compile time <em>if</em> that is <strong>guaranteed</strong> not to have any runtime overhead what so ever (no function jumps, etc etc).</p> <p>It is however possible, though the code will be quite tedious to maintain if you feel like adding another option to it (in C++03). I'd recommend you to check out the below solutions.</p> <hr> <h3>Solution in C++03</h3> <p>Your compiler should be smart enough to optimize away any call to <code>LogHelper&lt;+NONE&gt;</code>, though if you are just looking for more readable code and not a superb performance gain this syntax is quite sweet.</p> <pre><code>enum LoggerType { NONE =0, DATE = (1&lt;&lt;0), TIME = (1&lt;&lt;1), PERCENT = (1&lt;&lt;2) }; template&lt;int&gt; void LogHelper (std::string&amp;); template&lt;&gt; inline void LogHelper&lt;+NONE&gt; (std::string&amp;) {} template&lt;&gt; inline void LogHelper&lt;+DATE&gt; (std::string&amp; s) {s += "1970-01-01 ";} template&lt;&gt; inline void LogHelper&lt;+TIME&gt; (std::string&amp; s) {s += "12:01:01 ";} template&lt;&gt; inline void LogHelper&lt;+PERCENT&gt; (std::string&amp; s) {s += "42% ";} template&lt;int LOG_FLAG = NONE&gt; struct Logger { static void log (std::string const&amp; description) { std::string s1; LogHelper&lt;DATE &amp; LOG_FLAG&gt; (s1); LogHelper&lt;TIME &amp; LOG_FLAG&gt; (s1); LogHelper&lt;PERCENT &amp; LOG_FLAG&gt; (s1); std::cerr.width (25); std::cerr &lt;&lt; s1 &lt;&lt; " &gt;&gt; " &lt;&lt; description &lt;&lt; std::endl; } }; </code></pre> <p>...</p> <pre><code>int main (int argc, char * argv[]) { Logger&lt;DATE|TIME|PERCENT&gt; foo_log; Logger&lt;TIME&gt; time_log; Logger&lt;&gt; no_log; time_log.log ("log objects initialized!"); foo_log .log ("using foo_log"); no_log .log ("about to terminate application"); } </code></pre> <p><strong>output</strong></p> <pre><code> 12:01:01 &gt;&gt; log objects initialized! 1970-01-01 12:01:01 42% &gt;&gt; using foo_log &gt;&gt; about to terminate application </code></pre> <hr> <h3>Solution using Variadic Templates (C++11)</h3> <pre><code>enum LoggerType { NONE, PERCENT, DATE, TIME }; template&lt;LoggerType T = NONE, LoggerType ... Next&gt; std::string LogHelper () { return LogHelper&lt;T&gt; () + "; " + LogHelper&lt;Next...&gt; (); } template&lt;&gt; std::string LogHelper&lt;NONE&gt; () {return ""; } template&lt;&gt; std::string LogHelper&lt;DATE&gt; () {return "1970-01-01";} template&lt;&gt; std::string LogHelper&lt;TIME&gt; () {return "00:01:42";} template&lt;&gt; std::string LogHelper&lt;PERCENT&gt; () {return "42%";} template&lt;LoggerType ... Types&gt; struct Logger { static void log (std::string const&amp; description) { std::cerr.width (25); std::cerr &lt;&lt; LogHelper&lt;Types...&gt; (); std::cerr &lt;&lt; " &gt;&gt; " &lt;&lt; description; std::cerr &lt;&lt; std::endl; } }; </code></pre> <p>...</p> <pre><code>int main (int argc, char * argv[]) { Logger&lt;DATE,TIME,PERCENT&gt; foo_log; Logger&lt;TIME&gt; time_log; Logger&lt;&gt; no_log; time_log.log ("log objects initialized!"); foo_log .log ("using foo_log"); no_log .log ("about to terminate application"); } </code></pre> <p><strong>output</strong></p> <pre><code> 00:01:42 &gt;&gt; log objects initialized! 1970-01-01; 00:01:42; 42% &gt;&gt; using foo_log &gt;&gt; about to terminate application </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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