Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>all, thanks for sharing the code, I just do a test, then Neil's method will still do the string formating, for example:</p> <pre><code>#include &lt;streambuf&gt; #include &lt;ostream&gt; #include &lt;iostream&gt; using namespace std; template &lt;class cT, class traits = std::char_traits&lt;cT&gt; &gt; class basic_nullbuf: public std::basic_streambuf&lt;cT, traits&gt; { typename traits::int_type overflow(typename traits::int_type c) { return traits::not_eof(c); // indicate success } }; template &lt;class cT, class traits = std::char_traits&lt;cT&gt; &gt; class basic_onullstream: public std::basic_ostream&lt;cT, traits&gt; { public: basic_onullstream(): std::basic_ios&lt;cT, traits&gt;(&amp;m_sbuf), std::basic_ostream&lt;cT, traits&gt;(&amp;m_sbuf) { init(&amp;m_sbuf); } private: basic_nullbuf&lt;cT, traits&gt; m_sbuf; }; typedef basic_onullstream&lt;char&gt; onullstream; typedef basic_onullstream&lt;wchar_t&gt; wonullstream; class MyClass { int a; friend ostream&amp; operator&lt;&lt; (ostream&amp;, MyClass const&amp;); }; ostream&amp; operator&lt;&lt;(ostream&amp; out,MyClass const&amp; b) { std::cout&lt;&lt;"call format function!!"; out &lt;&lt; b.a; return out; } int main() { onullstream os; MyClass obj; os&lt;&lt;obj; } </code></pre> <p>Running this program, you will find that "ostream&amp; operator&lt;&lt;(ostream&amp; out,MyClass const&amp; b)" will be called. So, doing format on the obj will still be called. So, we still can't avoid the overhead of formatting messages.</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