Boost C++ Libraries: Ticket #9603: boost::optional operator<< does the wrong thing https://svn.boost.org/trac10/ticket/9603 <pre class="wiki">template&lt;class CharType, class CharTrait, class T&gt; inline std::basic_ostream&lt;CharType, CharTrait&gt;&amp; operator&lt;&lt;(std::basic_ostream&lt;CharType, CharTrait&gt;&amp; out, optional&lt;T&gt; const&amp; v) { if ( out.good() ) { if ( !v ) out &lt;&lt; "--" ; else out &lt;&lt; ' ' &lt;&lt; *v ; } return out; } </pre><p> This is the wrong semantics, in my opinion. Example of the problem: </p> <pre class="wiki">std::string s("--"); boost::optional&lt;std::string&gt; o; std::cout &lt;&lt; s &lt;&lt; std::endl; std::cout &lt;&lt; o &lt;&lt; std::endl; </pre><p> prints the same, which is clearly not intended. If the optional is not engaged (no value), printing should to nothing, i.e. return the stream unmodified. Also, the ' ' character in the code above should be removed. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9603 Trac 1.4.3 Steven Watanabe Mon, 10 Mar 2014 23:01:42 GMT <link>https://svn.boost.org/trac10/ticket/9603#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9603#comment:1</guid> <description> <p> It doesn't quite print the same thing. " --" is not the same as "--". I suspect that's the reason for the ' '. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>akrzemi1</dc:creator> <pubDate>Wed, 18 Feb 2015 14:38:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/9603#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/9603#comment:2</guid> <description> <p> The last requirement is invalid, the preceding space is used precisely to tell apart a valid value '--' form an invalid value. Therefore, there is a way of telling valid from invalid values. While it is a bit confusing, any adopted model would be confusing. For a detailed discussion, See here: <a class="ext-link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3527.html#rationale.io"><span class="icon">​</span>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3527.html#rationale.io</a> Your expectation that no output should be printed for empty optional is just one of the options which is not better than any other. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>akrzemi1</dc:creator> <pubDate>Wed, 18 Feb 2015 14:38:30 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/9603#comment:3 https://svn.boost.org/trac10/ticket/9603#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket