• <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 老馬驛站 閱讀(1263) 評論(0)  編輯 收藏 引用 所屬分類: Boost
            国产99久久久国产精品~~牛| 人人狠狠综合久久亚洲婷婷| 久久久久久无码国产精品中文字幕 | 深夜久久AAAAA级毛片免费看| 青春久久| 日韩精品久久久久久久电影蜜臀| 国产91久久精品一区二区| 日本福利片国产午夜久久| 午夜精品久久久久久影视777 | 国产成人无码精品久久久性色| 久久久久久无码Av成人影院| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲精品无码久久毛片| 999久久久免费精品国产| 青春久久| 日日狠狠久久偷偷色综合免费 | 久久久精品日本一区二区三区| 人妻无码αv中文字幕久久| 国产精品综合久久第一页| 亚洲伊人久久精品影院| 老司机午夜网站国内精品久久久久久久久 | 91精品国产91久久久久久| 综合网日日天干夜夜久久| 午夜视频久久久久一区 | 日韩美女18网站久久精品| 狠狠色婷婷综合天天久久丁香| 伊人久久综合精品无码AV专区| 伊人热人久久中文字幕| 久久久久亚洲AV无码专区体验| 亚洲国产成人久久综合碰| 久久久久亚洲精品男人的天堂| 久久精品国产一区| 久久亚洲国产精品一区二区| 丰满少妇人妻久久久久久| 亚洲国产欧美国产综合久久| 久久强奷乱码老熟女网站| 久久综合九色综合欧美就去吻| 久久久99精品成人片中文字幕| 麻豆国内精品久久久久久| 三级韩国一区久久二区综合| 一日本道伊人久久综合影|