Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><code>std::ostringstream()</code> is an rvalue, but <code>operator&lt;&lt;</code> takes an lvalue as first argument</p> </blockquote> <p>There's a generic inserter to take rvalue streams, but it returns a <code>basic_ostream&lt;charT, traits&gt;&amp;</code>:</p> <pre><code>template &lt;class charT, class traits, class T&gt; basic_ostream&lt;charT, traits&gt;&amp; operator&lt;&lt;(basic_ostream&lt;charT, traits&gt;&amp;&amp; os, const T&amp; x); </code></pre> <p>To work correctly for your example it would have to return the derived type of the stream (<code>std::ostringstram</code>).</p> <blockquote> <ol> <li>Is the output operator as I wrote it indeed working as expected (and fulfilling all the requirements I gave)? Or could it fail in unexpected ways or have other unexpected consequences? Especially: Is my use of <code>std::forward</code> correct here?</li> </ol> </blockquote> <p>Your code looks correct to me.</p> <blockquote> <ol> <li>Is there an easier way to meet the requirements?</li> </ol> </blockquote> <p> Your code looks similar to the code I wrote to solve this problem (see below).</p> <blockquote> <ol> <li>Assuming this is indeed correct and the simplest way to do it: Do you consider it a good idea to do that, or would you advice against it (both specifically for stream output as in my example, and as a more general scheme for passing objects through functions)?</li> </ol> </blockquote> <p>The idiom looks fine to me.</p> <p>In <a href="http://libcxx.llvm.org/" rel="nofollow">libc++</a> I defined the rvalue ostream inserter like the following (as an extension). I made an attempt to get it standardized but it was late and the committee was understandably not in the mood for more thrashing:</p> <pre><code>template &lt;class _Stream, class _Tp&gt; inline typename enable_if &lt; !is_lvalue_reference&lt;_Stream&gt;::value &amp;&amp; is_base_of&lt;ios_base, _Stream&gt;::value, _Stream&amp;&amp; &gt;::type operator&lt;&lt;(_Stream&amp;&amp; __os, const _Tp&amp; __x) { __os &lt;&lt; __x; return std::move(__os); } </code></pre> <p>Unlike yours, this <strong>only</strong> accepts rvalue streams. But like yours, it returns the concrete derived type, and so works with your example.</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.
    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