title: Clang: expression result unused with ternary operator
body: <p>To print debug messages in my program, I have a that can be used like this:</p>
  
  <pre><code>DBG(5) &lt;&lt; "Foobar" &lt;&lt; std::endl;
  </code></pre>
  
  <p>5 means the level of the message, if the debug level is smaller than 5, it won't print the message. Currently it is implemented like:</p>
  
  <pre><code>#define DBG(level) !::Logger::IsToDebug((level)) ? : ::Logger::Debug
  </code></pre>
  
  <p>Basically <code>IsToDebug</code> checks if the message should be printed, and returns true when it should.  <code>Logger::Debug</code> is an std::ostream. This works with gcc and clang too, however clang generates expression result unused warnings. According to <a href="http://lists.cs.uiuc.edu/pipermail/cfe-dev/2007-August/000166.html" rel="nofollow">this email</a> this doesn't like to change either.</p>
  
  <p>Prefixing it with <code>(void)</code> doesn't work, it will only cast the thing before the <code>?</code>, resulting in a compilation error (void can't be converted to bool, obviously). The other problem with this syntax that it uses a <a href="http://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals" rel="nofollow">gcc extension</a>.</p>
  
  <p>Doing things like <code>#define DBG(x) if (::Logger::IsToDebug((x))) ::Logger::Debug</code> solves the problem, but it's a sure way break your program (<code>if (foo) DBG(1) &lt;&lt; "foo"; else ...</code>) (and I can't put the whole thing into a <code>do { ... } while(0)</code> due to how the macro is called.)</p>
  
  <p>The only more or less viable solution I came up with is this (assuming <code>IsToDebug</code> returns either 0 or 1):</p>
  
  <pre><code>#define DBG(level) for(int dbgtmpvar = ::Logger::IsToDebug((level)); \
                         dbgtmpvar &gt; 0; --dbgtmpvar) ::Logger::Debug
  </code></pre>
  
  <p>Which seems like an overkill (not counting it's runtime overhead)</p>