• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-167  評論-8  文章-0  trackbacks-0

            Source

            #include <cstddef>

            #include <string>

            #include <typeinfo>

            //#include <boost/config.hpp>

            #include <boost/limits.hpp>

            #include <boost/throw_exception.hpp>

            #include <boost/type_traits/is_pointer.hpp>

            #include <sstream>

             

             

             

            //

            namespace kimi_boost

            {

                // exception used to indicate runtime lexical_cast failure

                class bad_lexical_cast : public std::bad_cast

                {

                public:

                    bad_lexical_cast() :

                    source(&typeid(void)), target(&typeid(void))

                    {

                    }

                    bad_lexical_cast(

                       const std::type_info &source_type,

                        const std::type_info &target_type) :

                        source(&source_type), target(&target_type)

                    {

                    }

                    const std::type_info &source_type() const

                    {

                        return *source;

                    }

                    const std::type_info &target_type() const

                    {

                        return *target;

                    }

                    virtual const char *what() const throw()

                    {

                        return "bad lexical cast: "

                               "source type value could not be interpreted as target";

                    }

                    virtual ~bad_lexical_cast() throw()

                    {

                    }

                private:

                    const std::type_info *source;

                    const std::type_info *target;

                };

             

             

               

                namespace detail // stream wrapper for handling lexical conversions

                {

                    template<typename Target, typename Source>

                    class lexical_stream

                    {

                    private:

                        typedef char char_type;

                                 std::basic_stringstream<char_type> stream;

             

                    public:

                        lexical_stream()

                        {

                            stream.unsetf(std::ios::skipws);

                            if(std::numeric_limits<Target>::is_specialized)

                                stream.precision(std::numeric_limits<Target>::digits10 + 1);

                            else if(std::numeric_limits<Source>::is_specialized)

                                stream.precision(std::numeric_limits<Source>::digits10 + 1);

                        }

             

                        ~lexical_stream()

                        {

                        }

             

                                 //Source類型輸入到流中

                        bool operator<<(const Source &input)

                        {

                            return !(stream << input).fail();

                        }

             

                                 //把流轉換為Target類型輸出

                        template<typename InputStreamable>

                        bool operator>>(InputStreamable &output)

                        {

                                        return !boost::is_pointer<InputStreamable>::value &&

                                   stream >> output &&

                                   stream.get() ==

                                       std::char_traits<char_type>::eof();

                        }

             

                                 //string特化

                                 template<>

                        bool operator>>(std::string &output)

                        {

                            output = stream.str();

                            return true;

                        }

                    };//class lexical_stream

                }//namespace detail

             

             

                namespace detail

                {

                    template<class T>

                    struct array_to_pointer_decay

                    {

                        typedef T type;

                    };

             

                    template<class T, std::size_t N>

                    struct array_to_pointer_decay<T[N]>

                    {

                        typedef const T * type;

                    };

                }

             

                template<typename Target, typename Source>

                Target lexical_cast(const Source &arg)

                {

                    typedef typename detail::array_to_pointer_decay<Source>::type NewSource;

             

                    detail::lexical_stream<Target, NewSource> interpreter;

                    Target result;

             

                    if(!(interpreter << arg && interpreter >> result))

                                 boost::throw_exception(bad_lexical_cast(typeid(NewSource), typeid(Target)));

                    return result;

                }

            }

            Test code

            void kimi_lexical_cast_test()

            {

                   try

                   {

                          int i=kimi_boost::lexical_cast<int>("4365");

                          float f=kimi_boost::lexical_cast<float>("234.546");

                          double d=kimi_boost::lexical_cast<double>("24534.546345");

                          std::string s=kimi_boost::lexical_cast<std::string>(24534.546345);

                   }

                   catch(kimi_boost::bad_lexical_cast& e)

                   {

                          cout<<e.what()<<endl;

                   }

             

                   try{

                          int i2=kimi_boost::lexical_cast<int>("0.335");

                   }

                   catch(kimi_boost::bad_lexical_cast& e)

                   {

                          cout<<"source type: "<<e.source_type().name()<<endl;

                          cout<<"target type: "<<e.target_type().name()<<endl;

                          cout<<e.what()<<endl;

                   }

            }

            Output

            source type: char const *

            target type: int

            bad lexical cast: source type value could not be interpreted as target

            posted on 2010-05-27 10:37 老馬驛站 閱讀(1274) 評論(0)  編輯 收藏 引用 所屬分類: Boost
            99久久夜色精品国产网站| 久久青青草原亚洲av无码app| 亚洲欧美一区二区三区久久| 色综合久久精品中文字幕首页| AV狠狠色丁香婷婷综合久久| 精品久久久无码人妻中文字幕豆芽 | 久久精品成人欧美大片| 国产精品久久波多野结衣| 久久se精品一区精品二区| 国产成人久久精品麻豆一区| 日韩欧美亚洲国产精品字幕久久久 | 日本三级久久网| 少妇熟女久久综合网色欲| 久久国产亚洲精品无码| 国内精品伊人久久久久影院对白| 欧美久久一区二区三区| 久久免费的精品国产V∧| 久久99精品九九九久久婷婷| 色妞色综合久久夜夜| 久久综合九色综合97_久久久| 欧美性大战久久久久久| 狠狠色丁香婷综合久久| 久久久一本精品99久久精品88| 丁香五月综合久久激情| www亚洲欲色成人久久精品| 精品熟女少妇av免费久久| 久久精品毛片免费观看| 亚洲国产精品18久久久久久| 久久久久久久亚洲Av无码| 精产国品久久一二三产区区别 | 99久久无色码中文字幕人妻| 久久久久国色AV免费看图片| 国产成人久久精品二区三区| 久久se精品一区精品二区国产| 国产成人精品久久一区二区三区| 99久久国产主播综合精品| 7国产欧美日韩综合天堂中文久久久久 | 亚洲AⅤ优女AV综合久久久| 国产亚洲美女精品久久久2020| 久久精品国产亚洲av高清漫画| 久久精品嫩草影院|