Boost C++ Libraries: Ticket #4009: lexical_cast to multimember object fails https://svn.boost.org/trac10/ticket/4009 <p> According to the documentation, the only relevant requirement for the lexical_cast&lt;target&gt;(source) is that the target is input-streamable. Having a simple class </p> <pre class="wiki">struct Foo { int fA; int fB; }; </pre><p> with input operator </p> <pre class="wiki">istream&amp; operator&gt;&gt;(istream&amp; is, Foo&amp; f) { return is &gt;&gt; f.fA &gt;&gt; f.fB; } </pre><p> should satisfy all the requirements. Nevertheless, the code </p> <pre class="wiki">const string s = "137 13"; const Foo f = lexical_cast&lt;Foo&gt;(s); </pre><p> compiles without any warnings (g++ 4.4.1) but eventually throws a bad_lexical_cast exception. Of course, using a non-optimized non-boost version of the lexical cast </p> <pre class="wiki">template&lt;typename T, typename U&gt; T LexicalCast(const U&amp; u) { stringstream ss; ss &lt;&lt; u; T t; if (ss &gt;&gt; t) return t; else throw bad_lexical_cast(); } </pre><p> works fine for the class in the example above. Are there additional requirements on the target type that are not mentioned in the documentation or is this a really severe bug? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4009 Trac 1.4.3 Darko Veberic <darko.veberic@…> Mon, 15 Mar 2010 20:14:25 GMT cc set https://svn.boost.org/trac10/ticket/4009#comment:1 https://svn.boost.org/trac10/ticket/4009#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">darko.veberic@…</span> added </li> </ul> Ticket Steven Watanabe Mon, 15 Mar 2010 21:49:25 GMT <link>https://svn.boost.org/trac10/ticket/4009#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4009#comment:2</guid> <description> <p> You need to use std::ws between the two integers, because Boost.<a class="missing wiki">LexicalCast</a> turns off skipping white space by default. </p> </description> <category>Ticket</category> </item> <item> <author>Darko Veberic <darko.veberic@…></author> <pubDate>Tue, 16 Mar 2010 08:12:20 GMT</pubDate> <title>status, severity changed; resolution set https://svn.boost.org/trac10/ticket/4009#comment:3 https://svn.boost.org/trac10/ticket/4009#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">fixed</span> </li> <li><strong>severity</strong> <span class="trac-field-old">Showstopper</span> → <span class="trac-field-new">Not Applicable</span> </li> </ul> <p> thanks, this solved the problem. the input operator thus has to be defined as </p> <pre class="wiki">istream&amp; operator&gt;&gt;(istream&amp; is, Foo&amp; f) { return is &gt;&gt; f.fA &gt;&gt; ws &gt;&gt; f.fB; } </pre><p> (what a n00b mistake...) </p> Ticket