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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久伊人高潮影院| 久久精品国产一区二区三区| 久久精品日日躁夜夜躁欧美| 蜜臀久久99精品久久久久久小说| 久久国产精品无码一区二区三区| 日本精品久久久中文字幕| 亚洲乱码日产精品a级毛片久久| 久久精品www人人爽人人| 久久国产视频99电影| 亚洲AV无码1区2区久久| 久久久久久亚洲精品无码| 久久国产精品99国产精| 欧美激情精品久久久久久久九九九| 婷婷久久香蕉五月综合加勒比| 国产精品欧美亚洲韩国日本久久 | 久久久久四虎国产精品| 久久这里的只有是精品23| 亚洲国产精品久久66| 亚洲午夜久久久久久噜噜噜| 久久青青草原精品国产不卡| 久久99国产综合精品免费| 久久久久久精品无码人妻| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲精品国产自在久久| 麻豆精品久久精品色综合| 久久婷婷激情综合色综合俺也去| 久久大香萑太香蕉av| 久久香蕉国产线看观看猫咪?v| 99久久99久久精品国产片果冻| 久久久av波多野一区二区| 久久久久久国产精品无码下载| 亚洲欧美成人久久综合中文网 | 久久天天躁狠狠躁夜夜avapp| 久久久久无码中| 久久综合五月丁香久久激情| 99久久精品九九亚洲精品| 亚洲午夜精品久久久久久人妖| 色综合久久久久网| 9191精品国产免费久久| 伊人久久综在合线亚洲2019| 精品国产一区二区三区久久蜜臀|