libstdc++-v3 HOWTO: Chapter 22

Simon Whomsley whomsley@avacadcam.com
Mon Jul 29 02:56:00 GMT 2002


There is a bug in the Toupper example code 
'http://gcc.gnu.org/onlinedocs/libstdc++/22_locale/howto.html'. In it, 
when constructing Toupper (in main***), the 'std::local("C")' is 
destructed immediately after the 'up'  is constructed causing 'up::loc' 
reference to be invalid when std::transform is called. (dito Tolower..)

Simon

   #include <iterator>    // for back_inserter
    #include <locale>
    #include <string>
    #include <algorithm>
    #include <cctype>      // old <ctype.h>

    struct Toupper
    {
        Toupper (std::locale const& l) : loc(l) {;}
        char operator() (char c)  { return std::toupper(c,loc); }
    private:
        std::locale const& loc;
    };

    struct Tolower
    {
        Tolower (std::locale const& l) : loc(l) {;}
        char operator() (char c)  { return std::tolower(c,loc); }
    private:
        std::locale const& loc;
    };

    int main ()
    {
       std::string  s ("Some Kind Of Initial Input Goes Here");
       Toupper      up   ( std::locale("C") );	// ***
       Tolower      down ( std::locale("C") );

       // Change everything into upper case
       std::transform (s.begin(), s.end(), s.begin(),
                       up
                      );

       // Change everything into lower case
       std::transform (s.begin(), s.end(), s.begin(),
                       down
                      );

       // Change everything back into upper case, but store the
       // result in a different string
       std::string  capital_s;
       std::transform (s.begin(), s.end(), std::back_inserter(capital_s),
                       up
                      );
    }

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1803 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20020729/18c8d997/attachment.bin>


More information about the Libstdc++ mailing list