Boost C++ Libraries: Ticket #5783: lexical_cast fails for types whose operator<</>> are templated on stream type https://svn.boost.org/trac10/ticket/5783 <p> The following code does not compile: </p> <pre class="wiki">#include &lt;boost/lexical_cast.hpp&gt; #include &lt;iostream&gt; #include &lt;string&gt; struct Foo { Foo() : f(2) {} int f; }; template &lt;typename OStream&gt; OStream&amp; operator&lt;&lt;(OStream&amp; ostr, const Foo&amp; foo) { ostr &lt;&lt; foo.f; return ostr; } int main() { Foo foo; // OK! std::cout &lt;&lt; foo &lt;&lt; std::endl; // Does not compile! std::cout &lt;&lt; boost::lexical_cast&lt;std::string&gt;(foo) &lt;&lt; std::endl; } </pre><p> The problem is that inside lexical_cast there is a stream-like type called lexical_stream_limited_src which has a bunch of operator&lt;&lt; overloads which all return bool, and the rest of the implementation relies on these being called, and treats "x &lt;&lt; y" expressions as returning bool. However, in the example above, such expressions match the template operator&lt;&lt; instead. </p> <p> I ran into this because boost::fusion uses operator&lt;&lt;s of this type. So, while it is possible to stream a fusion::pair to an ostream, it is not possible to lexical_cast it to string. </p> <p> One solution would be to get rid of the bool return type for lexical_stream_limited_src::operator&lt;&lt;, and instead for these methods to save in a boolean member variable whether the call succeeded or failed, and for client code of this class to check the member after the call. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5783 Trac 1.4.3 Antony Polukhin Tue, 16 Aug 2011 16:34:23 GMT owner, status, severity, milestone changed https://svn.boost.org/trac10/ticket/5783#comment:1 https://svn.boost.org/trac10/ticket/5783#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">nasonov</span> to <span class="trac-author">Antony Polukhin</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>severity</strong> <span class="trac-field-old">Problem</span> → <span class="trac-field-new">Regression</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.48.0</span> </li> </ul> Ticket Antony Polukhin Tue, 16 Aug 2011 17:05:43 GMT <link>https://svn.boost.org/trac10/ticket/5783#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5783#comment:2</guid> <description> <p> Great thanks for reporting this bug! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Tue, 16 Aug 2011 17:09:27 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5783#comment:3 https://svn.boost.org/trac10/ticket/5783#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/73818" title="Fixes #5783 Fixes ambiguities with operators &gt;&gt; and &lt;&lt;. Adds tests ...">[73818]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5783" title="#5783: Bugs: lexical_cast fails for types whose operator&lt;&lt;/&gt;&gt; are templated on ... (closed: fixed)">#5783</a> </p> <p> Fixes ambiguities with operators &gt;&gt; and &lt;&lt;. Adds tests for such cases. </p> Ticket