Note that there are some explanatory texts on larger screens.

plurals
  1. POSupport for const_string in std::ostream operator <<
    primarykey
    data
    text
    <p>I'm currently using the very clever package <a href="http://conststring.sourceforge.net/" rel="nofollow"><code>boost::const_string</code></a> until <a href="http://libcxx.llvm.org/" rel="nofollow">http://libcxx.llvm.org/</a> is available pre-packaged on Ubuntu or GCC make its <code>__versa_string</code> (in header <code>ext/vstring.h</code>) its default string implementation. libcxx's <code>std::string</code> aswell as <code>__versa_string</code> uses _small-string optimization (SSO) by default. Default support for outputting to an <code>std::ostream</code> is lacking however. The code</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/const_string.hpp&gt; const_string&lt;char&gt; x; std::cout &lt;&lt; x &lt;&lt; endl; </code></pre> <p>does not work unless we force <code>x</code> into a c-string via <code>c_str()</code> which becomes</p> <pre><code>std::cout &lt;&lt; x.c_str() &lt;&lt; endl; </code></pre> <p>which compiles and works as expected. I added the following line to <code>const_string.hpp</code></p> <pre><code>template &lt;typename T&gt; inline std::ostream &amp; operator &lt;&lt; (std::ostream &amp; os, const boost::const_string&lt;T&gt; &amp; a) { return os.write(a.data(), a.size()); } </code></pre> <p>This should improve performance over <code>x.c_str()</code> because <code>size()</code> is already known and does not need to be calculated by searching for <code>NULL</code> as in <code>c_str()</code>. I works for me but I am uncertain whether it works all cases. Have I missed something?</p>
    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.
 

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