title: Function that prints something to std::ostream and returns std::ostream?
body: <p>I want to write a function that outputs something to a <code>ostream</code> that's passed in, and return the stream, like this:</p>
  
  <pre><code>std::ostream&amp; MyPrint(int val, std::ostream* out) {
    *out &lt;&lt; val;
    return *out;
  }
  
  int main(int argc, char** argv){
      std::cout &lt;&lt; "Value: " &lt;&lt; MyPrint(12, &amp;std::cout) &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>It would be convenient to print the value like this and embed the function call in the output operator chain, like I did in <code>main()</code>. </p>
  
  <p>It doesn't work, however, and prints this:</p>
  
  <pre><code>$ ./a.out
  12Value: 0x6013a8
  </code></pre>
  
  <p>The desired output would be this:</p>
  
  <pre><code>Value: 12
  </code></pre>
  
  <p>How can I fix this? Do I have to define an <code>operator&lt;&lt;</code> instead?</p>
  
  <p><strong>UPDATE:</strong> Clarified what the desired output would be.</p>
  
  <p><strong>UPDATE2:</strong> Some people didn't understand why I would print a number like that, using a function instead of printing it directly. This is a simplified example, and in reality the function prints a complex object rather than an <code>int</code>. </p>