Note that there are some explanatory texts on larger screens.

plurals
  1. POPerfect pass-through
    text
    copied!<p>I'm thinking about a problem which has some similarity with perfect forwarding, but where the function argument is not passed to a called function, but returned. This is why I call it "perfect pass-through".</p> <p>The problem is the following:</p> <p>Say we have a function which takes an object by reference (and possibly some extra arguments), modifies that object, and returns the modified object. The best-known example of such functions are probably <code>operator&lt;&lt;</code> and <code>operator&gt;&gt;</code> for iostreams.</p> <p>Let's use iostreams as example, because it allows to nicely show what I'm after. For example, one thing which one would sometimes like to do is:</p> <pre><code>std::string s = (std::ostringstream() &lt;&lt; foo &lt;&lt; bar &lt;&lt; baz).str(); </code></pre> <p>Of course that doesn't work, for two reasons:</p> <ul> <li><p><code>std::ostringstream()</code> is an rvalue, but <code>operator&lt;&lt;</code> takes an lvalue as first argument</p></li> <li><p><code>operator&lt;&lt;</code> returns an <code>ostream&amp;</code> (well, at least for the standard ones actually a <code>basic_ostream&lt;CharT, Traits&gt;&amp;</code> where <code>CharT</code> and <code>Traits</code> are deduced from the first argument).</p></li> </ul> <p>So let's assume we want to design the insertion operator so that the above works (you obviously can't do that for the existing operators, but you can do that for your own classes). Obviously the solution should have the following traits:</p> <ul> <li><p>The first argument can accept either an lvalue or an rvalue.</p></li> <li><p>The return type should be the same type as passed in. But of course it should still only accept ostreams (i.e. classes derived from an instantiation of <code>basic_ostream</code>).</p></li> </ul> <p>While in this specific use case it isn't needed, I want to add a third requirement:</p> <ul> <li>If the first argument is an rvalue, so is the returned value, otherwise an lvalue is returned.</li> </ul> <p>This extra rule is so that you can move-construct from an rvalue passed through the function (I don't know if C++11 streams are move constructible, but it is intended to be a more general scheme, with the stream just as handy example).</p> <p>It is obvious that in C++03, those requirements could not all be met. However, in C++11, we have rvalue references, which should make this possible.</p> <p>Here's my try on this:</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; template&lt;typename Ostream&gt; struct is_ostream { typedef typename std::remove_reference&lt;Ostream&gt;::type candidate; typedef typename candidate::char_type char_type; typedef typename candidate::traits_type traits_type; typedef std::basic_ostream&lt;char_type, traits_type&gt; basic_ostream; static const bool value = std::is_base_of&lt;basic_ostream, candidate&gt;::value; }; class SomeType {}; template&lt;typename Ostream&gt; typename std::enable_if&lt;is_ostream&lt;Ostream&gt;::value, Ostream&amp;&amp;&gt;::type operator&lt;&lt;(Ostream&amp;&amp; is, SomeType const&amp; x) { is &lt;&lt; "SomeType"; return std::forward&lt;Ostream&gt;(is); } int main() { SomeType t; std::string s = (std::ostringstream() &lt;&lt; t).str(); std::cout &lt;&lt; s &lt;&lt; std::endl; } </code></pre> <p>It indeed compiles with gcc (using the option <code>-std=c++0x</code>), and when run outputs <code>SomeType</code> as expected. However, it is a quite complicated machinery to get there.</p> <p>Therefore my questions:</p> <ol> <li><p>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?</p></li> <li><p>Is there an easier way to meet the requirements?</p></li> <li><p>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)?</p></li> </ol>
 

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