• <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 丑石 閱讀(796) 評論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久亚洲AV无码麻豆| 国产日韩欧美久久| 人妻少妇久久中文字幕| 久久AAAA片一区二区| 伊人久久综合热线大杳蕉下载| 久久久久99精品成人片直播| 色综合久久中文色婷婷| 99久久精品国产毛片| 伊人久久五月天| 久久婷婷五月综合色奶水99啪| 色综合久久久久综合体桃花网| 人妻无码久久一区二区三区免费| 久久ZYZ资源站无码中文动漫| 久久久久无码精品国产| 久久91这里精品国产2020| 欧美黑人激情性久久| 77777亚洲午夜久久多喷| 欧美久久久久久午夜精品| 国产69精品久久久久9999APGF| 99久久精品费精品国产一区二区| 国产A级毛片久久久精品毛片| 蜜桃麻豆WWW久久囤产精品| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 久久99精品久久久久久秒播| 久久久精品国产| 日批日出水久久亚洲精品tv| 久久亚洲精品视频| 99久久精品毛片免费播放| 国产亚洲精品久久久久秋霞 | 久久久久久久久久久精品尤物| 色偷偷88888欧美精品久久久| 四虎国产精品成人免费久久| 国产精品久久一区二区三区 | 欧美黑人又粗又大久久久| 99久久这里只精品国产免费| 久久国产精品免费一区| 成人a毛片久久免费播放| 狠狠久久亚洲欧美专区 | 精品九九久久国内精品| 久久久久亚洲AV无码麻豆| 麻豆精品久久精品色综合|