Boost C++ Libraries: Ticket #4397: specialization for lexical_cas for identical Target and Source types https://svn.boost.org/trac10/ticket/4397 <p> std::string s1 = "str"; std::string result = boost::lexical_cast&lt;std::string&gt;(s1); </p> <p> In this case it is possible to use specialization of lexical_cast which just returns input arg. </p> <p> I created simple example with specialization of lexical_cast: </p> <pre class="wiki">#include &lt;boost/cstdint.hpp&gt; #include &lt;boost/lexical_cast.hpp&gt; #include &lt;boost/type_traits.hpp&gt; #include &lt;iostream&gt; template &lt;typename Target, typename Source, bool val&gt; struct helper { /// Default action is to use lexical_cast static Target get(const Source &amp; src) { return boost::lexical_cast&lt;Target&gt;(src); } }; template &lt;typename Target, typename Source&gt; struct helper&lt;Target, Source, true&gt; { /// Use this if Target and Source identical static Target get(const Source &amp; src) { return src; } }; template &lt;typename Target, typename Source&gt; Target my_lexical_cast(const Source &amp; res) { /// Use boost::is_same for checking identity of Target and Source return helper&lt;Target, Source, boost::is_same&lt;Target, Source&gt;::value&gt;::get(res); } int main(int argc, char * argv[]) { std::string b1 = "10"; std::string b2 = my_lexical_cast&lt;std::string&gt;(b1); int b3 = my_lexical_cast&lt;int&gt;(b1); std::cout &lt;&lt; "b1: " &lt;&lt; b1 &lt;&lt; std::endl; std::cout &lt;&lt; "b2: " &lt;&lt; b2 &lt;&lt; std::endl; std::cout &lt;&lt; "b3: " &lt;&lt; b3 &lt;&lt; std::endl; return 0; } </pre><p> This code checked in msvc 9.0, mingw32-g++ 4.4.0 </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4397 Trac 1.4.3 alexey.kutumov@… Fri, 02 Jul 2010 08:04:33 GMT attachment set https://svn.boost.org/trac10/ticket/4397 https://svn.boost.org/trac10/ticket/4397 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">conv_test.cpp</span> </li> </ul> <p> Simple test for performance of specialization of lexical_cast </p> Ticket alexey.kutumov@… Fri, 02 Jul 2010 08:11:30 GMT <link>https://svn.boost.org/trac10/ticket/4397#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4397#comment:1</guid> <description> <p> I compiled given test under msvc 2008, and my_lexical_cast is two times faster then usual boost::lexical_cast </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 13 May 2011 09:56:27 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/4397#comment:2 https://svn.boost.org/trac10/ticket/4397#comment:2 <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> </ul> Ticket Antony Polukhin Sun, 15 May 2011 15:31:04 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4397#comment:3 https://svn.boost.org/trac10/ticket/4397#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/71958" title="Fixes #5417. Fixes #4397 More tests (for conversions to float types, ...">[71958]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5350" title="#5350: Patches: Patch for lexical_cast: additional optimizations (closed: fixed)">#5350</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4397" title="#4397: Feature Requests: specialization for lexical_cas for identical Target and Source types (closed: fixed)">#4397</a> More tests (for conversions to float types, for conversions of negative values to unsigned integers) </p> Ticket Antony Polukhin Sun, 15 May 2011 15:42:54 GMT <link>https://svn.boost.org/trac10/ticket/4397#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4397#comment:4</guid> <description> <p> Thanks for the idea. <br /> It was redesigned and implemented in <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5350" title="#5350: Patches: Patch for lexical_cast: additional optimizations (closed: fixed)">#5350</a>, <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5350" title="#5350: Patches: Patch for lexical_cast: additional optimizations (closed: fixed)">#5350</a> was merged to trunk (with some fixes for behavior described in <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5494" title="#5494: Bugs: lexical_cast&lt;unsigned int&gt;(&#34;-1&#34;) fails to throw (closed: wontfix)">#5494</a> and more optimizations for conversions from arithmetic to arithmetic types). </p> </description> <category>Ticket</category> </item> </channel> </rss>