• <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();

                        }

             

                                 //把流轉(zhuǎn)換為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 老馬驛站 閱讀(1256) 評論(0)  編輯 收藏 引用 所屬分類: Boost
            久久综合伊人77777麻豆| 亚洲国产成人久久一区久久| 久久精品18| 88久久精品无码一区二区毛片 | 亚洲成色999久久网站| 国产一区二区三区久久| 日本欧美久久久久免费播放网| 一本色道久久综合| 久久亚洲国产成人影院网站| 国产精品伊人久久伊人电影| 久久亚洲高清观看| 成人精品一区二区久久| 爱做久久久久久| 久久天天躁狠狠躁夜夜不卡| 久久久久无码专区亚洲av| 久久久青草青青国产亚洲免观| 精品国产91久久久久久久a| 久久强奷乱码老熟女| 久久婷婷人人澡人人爽人人爱| 亚洲国产精品高清久久久| 天堂久久天堂AV色综合| 丰满少妇人妻久久久久久| 91麻豆精品国产91久久久久久| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久久久亚洲精品中文字幕| 国产精品中文久久久久久久| 日日噜噜夜夜狠狠久久丁香五月| 国产91色综合久久免费分享| 欧美精品一区二区精品久久| 久久久精品久久久久特色影视| 久久人人爽人人爽人人片AV东京热 | 久久99国产精品二区不卡| 久久久久亚洲AV成人网人人软件| 久久中文字幕人妻熟av女| 2020久久精品国产免费| 亚洲人成无码久久电影网站| 成人久久精品一区二区三区| 一97日本道伊人久久综合影院| 久久无码av三级| 亚洲国产精品无码久久一线| 久久精品亚洲乱码伦伦中文|