[Boost-bugs] [Boost C++ Libraries] #4397: specialization for lexical_cas for identical Target and Source types

Subject: [Boost-bugs] [Boost C++ Libraries] #4397: specialization for lexical_cas for identical Target and Source types
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-07-02 07:25:12


#4397: specialization for lexical_cas for identical Target and Source types
-----------------------------------------+----------------------------------
 Reporter: alexey.kutumov@… | Owner: nasonov
     Type: Feature Requests | Status: new
Milestone: Boost 1.44.0 | Component: lexical_cast
  Version: Boost 1.44.0 | Severity: Optimization
 Keywords: lexical_cast specialization |
-----------------------------------------+----------------------------------
 std::string s1 = "str";
 std::string result = boost::lexical_cast<std::string>(s1);

 In this case it is possible to use specialization of lexical_cast which
 just returns input arg.

 I created simple example with specialization of lexical_cast:


 {{{
 #include <boost/cstdint.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/type_traits.hpp>
 #include <iostream>

 template <typename Target, typename Source, bool val> struct helper
 {
         /// Default action is to use lexical_cast
         static Target get(const Source & src)
         {
                 return boost::lexical_cast<Target>(src);
         }
 };

 template <typename Target, typename Source> struct helper<Target, Source,
 true>
 {

         /// Use this if Target and Source identical
         static Target get(const Source & src)
         {
                 return src;
         }
 };

 template <typename Target, typename Source> Target my_lexical_cast(const
 Source & res)
 {
         /// Use boost::is_same for checking identity of Target and Source
         return helper<Target, Source, boost::is_same<Target,
 Source>::value>::get(res);
 }

 int main(int argc, char * argv[])
 {
         std::string b1 = "10";

         std::string b2 = my_lexical_cast<std::string>(b1);
         int b3 = my_lexical_cast<int>(b1);

         std::cout << "b1: " << b1 << std::endl;
         std::cout << "b2: " << b2 << std::endl;
         std::cout << "b3: " << b3 << std::endl;

         return 0;
 }
 }}}



 This code checked in msvc 9.0, mingw32-g++ 4.4.0

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/4397>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:03 UTC