• <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久久国产综合精品五月天喷水| 国内精品久久久久久久久| 久久久久久午夜精品| 思思久久精品在热线热| 国产亚洲美女精品久久久2020| 无码AV中文字幕久久专区| 好久久免费视频高清| 午夜精品久久久久久影视777| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 一级女性全黄久久生活片免费| 久久免费看黄a级毛片| 久久精品9988| 精品综合久久久久久98| 久久免费国产精品一区二区| 中文字幕无码久久精品青草| 九九99精品久久久久久| 狠狠色丁香久久婷婷综合图片| 久久99国产精品久久99| 亚洲AV无码久久精品成人| 国产精品成人99久久久久 | 久久93精品国产91久久综合| 亚洲精品乱码久久久久久蜜桃图片| 日本久久久精品中文字幕| 亚洲日韩中文无码久久| 久久久久久国产精品美女| 99久久人妻无码精品系列蜜桃| 久久婷婷国产剧情内射白浆| 久久精品无码一区二区三区日韩| 精品少妇人妻av无码久久| 狠狠色噜噜色狠狠狠综合久久 | 国内精品久久久久影院薰衣草| 中文字幕一区二区三区久久网站 | 狠狠色婷婷久久综合频道日韩| 欧美精品福利视频一区二区三区久久久精品 | 亚洲国产天堂久久久久久| 国产三级精品久久| 久久久久99精品成人片| 久久播电影网| 亚洲国产成人久久综合碰| 人人狠狠综合久久亚洲| 日本久久中文字幕|