• <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>

            linux&c++ R&D

            programing is a pleasure!

            Using std::basic_string the extensible way

            Problem:
            Using std::basic_string the extensible way
            I'm writing a class that contains a string. But I decided to make it
            more extensible and turn it into a class template:
            template<typename CharT>
            class My
            {
                 std::basic_string<CharT> text;
            };

            How do I operate on such a string, so that it will work for any valid
            instantiation of basic_string? For example, I'd like to append some text
            to the end of the string or check if the first letter is lowercase.
            Currently I'm doing:
                 text += "some other text";
            For wstring I should probably do:
                 test += L"some other text";
            How do I do it in a generic way?
            Solution:
            That depends on the meaning of these literals.
            Are these only some selected literals of the
            corresponding character type with a special
            meaning? If so, than a character-depending
            policy/traits class would be reasonable, which
            would be explicitely specialized for the corresponding
            value, e.g.
            template <typename CharT> 
            class MyTraits; 
            template 
            <> 
            struct MyTraits
            <char> 
              
            static const char* append() 
                
            return "some other text"
              }

            }

            template 
            <> 
            struct MyTraits
            <wchar_t> 
              
            static const wchar_t* append() 
                
            return L"some other text"
              }
             
            }

            template
            <typename CharT> 
            class My 

               std::basic_string
            <CharT> text; 
               typedef MyTraits
            <CharT> Traits; 
               
            void foo() 
                  text 
            += Traits::append(); 
               }
             
            }


            Otherwise you could only declare the member
            functions in the My class template and explicitely
            specialize them for the corresponding character
            type:
            template<typename CharT> 
            class My 

               std::basic_string
            <CharT> text; 
            public
               
            void foo(); // Only declared, not defined 
            }

            template
            <> 
            void My<char>::foo() 
              text 
            += "some other text"
            }
             
            template
            <> 
            void My<wchar_t>::foo() 
              text 
            += L"some other text"
            }
             

            In both cases some preprocessor magic
            can help reducing the redundancies.
            ----------------------------------------------------------------------------
            How about writing a helper function to convert a narrow string literal
            to an arbitrary basic_string<T>: 
               // A template to 'widen' a string literal 
               
            template < class CharT > 
               std::basic_string
            <CharT> widen( char const* str ) 
                 std::basic_stringstream
            < CharT > s; 
                 s 
            << str; return s.str(); 
               }
             
            Then you can 
            do 
               test 
            += widen<char>"foo bar" ); 
            or 
               test 
            += widen<wchar_t>"foo bar" ); 

            as appropriate.
            You could even make it a constructor, but it's probably best to make it
            explicit in the code when you want this widening.

            posted on 2007-04-25 23:39 丑石 閱讀(800) 評論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            国产午夜精品久久久久九九| 99久久免费国产精品热| 精品欧美一区二区三区久久久| 久久久久亚洲AV无码专区体验| 99久久成人18免费网站| 国内精品九九久久精品| 国内精品久久久久国产盗摄| 久久精品国产精品亚洲艾草网美妙| 久久精品国产亚洲AV无码麻豆| 精品午夜久久福利大片| 色欲久久久天天天综合网精品| 久久久久亚洲av毛片大| 久久亚洲国产午夜精品理论片| 一级女性全黄久久生活片免费| 精品国产福利久久久| 一本一道久久精品综合| 欧美亚洲国产精品久久| 久久久久一本毛久久久| 亚洲国产精品无码久久久蜜芽| 国产精品午夜久久| 一本伊大人香蕉久久网手机| 亚洲国产精品无码久久一区二区| 久久久精品久久久久特色影视| 久久精品一本到99热免费| 久久毛片一区二区| 久久国产综合精品五月天| 久久久久久久99精品免费观看| 少妇久久久久久久久久| 国产精品久久久久久五月尺| 国产精品一区二区久久精品涩爱 | 日韩十八禁一区二区久久| 久久高潮一级毛片免费| 亚洲国产成人久久精品影视| 久久久无码人妻精品无码| 亚洲香蕉网久久综合影视| 久久笫一福利免费导航| 日本WV一本一道久久香蕉| 久久婷婷人人澡人人爽人人爱| 国色天香久久久久久久小说| 18禁黄久久久AAA片| 久久久www免费人成精品|