• <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
            欧美精品一本久久男人的天堂| 人妻无码精品久久亚瑟影视| 久久久久亚洲AV无码专区体验| 欧美噜噜久久久XXX| 国产 亚洲 欧美 另类 久久| 亚洲精品第一综合99久久| 久久精品夜夜夜夜夜久久| 国产精品久久久久一区二区三区 | 欧美亚洲色综久久精品国产| 99久久中文字幕| 人人妻久久人人澡人人爽人人精品| 久久综合国产乱子伦精品免费| 91性高湖久久久久| 亚洲va久久久噜噜噜久久男同| 欧美精品一本久久男人的天堂| 欧美激情一区二区久久久| 欧美伊香蕉久久综合类网站| 欧美日韩久久中文字幕| 久久国产影院| 日本久久久精品中文字幕| 色狠狠久久AV五月综合| 午夜精品久久久久久久无码| 久久91精品国产91久久麻豆| 国内精品久久久久久久久电影网| 亚洲精品高清久久| AV无码久久久久不卡蜜桃| 97精品依人久久久大香线蕉97| 欧美久久一级内射wwwwww.| 精品久久久久久久| 欧美一区二区三区久久综合| 亚洲精品无码久久久影院相关影片 | 91精品国产91久久久久久蜜臀 | 思思久久99热只有频精品66| 国产精品亚洲美女久久久| 久久香蕉一级毛片| 91精品久久久久久无码| 九九久久99综合一区二区| 日本道色综合久久影院| 久久精品国产69国产精品亚洲| 久久av无码专区亚洲av桃花岛| 久久久精品人妻一区二区三区四|