Boost C++ Libraries: Ticket #244: Bug in lexical_cast.hpp https://svn.boost.org/trac10/ticket/244 <pre class="wiki">In boost-1_31_0, I believe there is a typo in lexical_cast.hpp The line Target lexical_cast(Source arg) should be Target lexical_cast(Source&amp; arg) Otherwise it actually creates a copy of the argument before doing the lexical cast. This causes problems if you try to lexical_cast() a virtual object. Here is code that shows the bug. By just adding the '&amp;', the code performs as you would expect it to. #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;boost/lexical_cast.hpp&gt; struct A { virtual int f() const = 0; }; struct B : A { virtual int f() const { return 1; } }; struct C : B { virtual int f() const { return 2; } }; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const A&amp; a) { return os &lt;&lt; a.f(); } int main() { C c; std::string s; std::cout &lt;&lt; c &lt;&lt; std::endl; //This should print 2 s = boost::lexical_cast&lt;std::string&gt;(c); std::cout &lt;&lt; s &lt;&lt; std::endl; //This should print 2 s = boost::lexical_cast&lt;std::string, B&gt;(c); std::cout &lt;&lt; s &lt;&lt; std::endl; //This returns 1, not 2 //This fails because it tries to instantiate an object of type A. Why??? s = boost::lexical_cast&lt;std::string, A&gt;(c); std::cout &lt;&lt; s &lt;&lt; std::endl; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/244 Trac 1.4.3 kevlin Thu, 18 Mar 2004 07:52:53 GMT status changed https://svn.boost.org/trac10/ticket/244#comment:1 https://svn.boost.org/trac10/ticket/244#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> <pre class="wiki">Logged In: YES user_id=32864 The pass-by-copy has always been intentional and is not a defect. </pre> Ticket jfmeinel Thu, 18 Mar 2004 15:54:27 GMT <link>https://svn.boost.org/trac10/ticket/244#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/244#comment:2</guid> <description> <pre class="wiki">Logged In: YES user_id=266114 Is there a discussion about this that I can read? I use boost::python, and the built-in str() function uses lexical cast to convert your object to a string. However, this ignores virtual functions, and doesn't allow lexical casts of abstract base classes. I realize there are possibly some dependencies on &lt;limits&gt; (which also requires non-abstract class), but are those actually necessary? I thought all you really needed defined was the &lt;&lt; operator, and lexical_cast should work. </pre> </description> <category>Ticket</category> </item> </channel> </rss>