• <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一综合| 四虎国产精品免费久久5151| 国产精品成人精品久久久 | 欧美黑人激情性久久| 久久精品久久久久观看99水蜜桃| 久久国产精品无码一区二区三区| 伊人热人久久中文字幕| 亚洲国产综合久久天堂| 国产精品99久久久久久人| 久久精品不卡| 久久精品国产亚洲av麻豆小说| 国产福利电影一区二区三区,免费久久久久久久精 | 精品无码久久久久久午夜| 久久精品无码av| 久久91精品国产91久久麻豆| 亚洲人成无码网站久久99热国产 | 亚洲一区精品伊人久久伊人 | 久久久久亚洲av无码专区导航| 久久AAAA片一区二区| 精品久久久久久综合日本| 99久久国产综合精品女同图片| 欧美久久一区二区三区| 国产一级持黄大片99久久| 久久人与动人物a级毛片| 久久AAAA片一区二区| 99久久精品国产麻豆| 久久久无码人妻精品无码| 久久天天躁夜夜躁狠狠躁2022| 欧美午夜A∨大片久久 | 伊人色综合久久天天人守人婷| 久久国产精品久久国产精品| 九九精品99久久久香蕉| 日韩久久久久久中文人妻| 一本久久a久久精品亚洲| 久久免费视频1| 久久午夜夜伦鲁鲁片免费无码影视| 久久亚洲高清综合| 久久夜色撩人精品国产小说| 久久影视国产亚洲| 久久久久久国产精品美女 |