? Bug in libstdc++? GCC 4.1: wcout.imbue(loc) should (?) set stream encoder's, but it doesn't; std::

Discussion in 'C++' started by year1943@front.ru, Sep 11, 2007.

  1. Guest

    There was the same topic not so long ago, but as I see it stays w/o
    answer:
    http://groups.google.ru/group/comp....&q=locale imbue&rnum=1&hl=ru#fe109c899f916871

    As Bjarne Stroustrup said in his book,
    "in Stroustrup (retranslated from German)
    "Setting the global locale does not affect existing input/output
    streams. The streams continue to use those locales that were assigned
    to
    them using imbue() during their creation." "

    But actually I see quite the opposite behaviour.
    The following code:

    #include <iostream>
    // #include <clocale>
    #include <locale>

    int main() {
    std::locale loc("") ;
    // std::setlocale(LC_ALL, "") ; // (1)
    std::locale::global(loc) ; // (2)
    // std::wcout.imbue(loc) ; // (3)
    std::wcout << L"(non-ASCII national letters here)" << std::endl ;

    std::wstring wstr ;
    std::wcin >> wstr ;
    std::wcout << L'[' << wstr << L']' << std::endl ;

    return 0 ;
    }

    DOES work (it correctly inputs and outputs non-ASCII, non-"C"-locale
    chars), while it SHOULD NOT (?), and if you commented out (1) and (2)
    and uncommented (3) - it DOESN'T work, though it SHOULD DO (as
    Stroustrup says).

    More over, if you switch global locale between points of wcout usage,
    like:

    int main() {
    std::locale loc("") ;
    std::wcout << L"(non-ASCII national letters here)" << std::endl ;
    std::locale::global(loc) ; // (2)
    std::wcout << L"(non-ASCII national letters here)" << std::endl ;

    - wcout DOESN'T WORK correctly even after global locale switch!!!

    What's wrong with my attempts to make it work??

    (I'm afraid, it's one more of numerous problems/bugs of C/C++
    alliance, but would happy to hear "official" answer and ways to
    workaround...)
    , Sep 11, 2007
    #1
    1. Advertising

  2. Guest

    > (I'm afraid, it's one more of numerous problems/bugs of C/C++
    > alliance, but would happy to hear "official" answer and ways to
    > workaround...)


    It seems (partially) right. Call to

    std::ios_base::sync_with_stdio(false) ;

    at the every beginning changes behaviour, but it still stays looking
    buggy or "impl defined":

    int main() {
    std::ios_base::sync_with_stdio(false) ;
    std::locale loc("") ;

    std::wcout << L"Hello world!" << std::endl ;
    std::wcout.imbue(loc) ;
    std::wcout << L" !" << std::endl ;

    this works fine, as expected.
    If you try to output non-ASCI chars with "C" locale on stream:

    std::wcout << L" !" << std::endl ;

    nothing is printed at all, even with flushing endl manip; following
    even ASCII strings also disappears silently to blackhole;
    but if later you call imbue:

    std::wcout << L" !" << std::endl ;
    std::wcout.imbue(loc) ;

    than you got -
    terminate called after throwing an instance of
    'std::ios_base::failure'
    what(): basic_filebuf::_M_convert_to_external conversion error
    Aborted

    It seems changing the global locale now doesn't affect stream:

    int main() {
    std::ios_base::sync_with_stdio(false) ;
    std::locale loc("") ;

    std::wcout << L"Hello world!" << std::endl ;
    std::locale::global(loc) ;
    // std::wcout << L" !" << std::endl ; // will cause
    exception
    std::wcout.imbue(loc) ;
    std::wcout << L" !" << std::endl ; // now OK
    std::locale::global(std::locale::classic()) ;
    std::wcout << L" !" << std::endl ; // OK, using
    stream's loc not global
    std::wcout.imbue(std::locale::classic()) ;
    // std::wcout << L" !" << std::endl ; // will cause
    exception
    std::wcout << L"Hello world!" << std::endl ; // OK


    Either Bjarne was inaccurate or at least not pedantic with his locale/
    stream descriptions... or libstdc++ is implemented in a such manner?..
    , Sep 11, 2007
    #2
    1. Advertising

  3. Guest

    Sorry, empty literals above, like
    L" !"
    are consisting of national non-ASCII chars, now we can't see them due
    to buggy unicode support by Google - Goo replaced alphabetic chars
    with spaces. :eek:)
    , Sep 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    6,637
    Victor Bazarov
    Jul 16, 2004
  2. Ralf Goertz

    mixing of std::cout and std::wcout

    Ralf Goertz, Jul 4, 2007, in forum: C++
    Replies:
    2
    Views:
    420
    Ralf Goertz
    Jul 6, 2007
  3. Alexander Dong Back Kim

    how to use libstdc++.so.5 instead of libstdc++.so.6

    Alexander Dong Back Kim, Sep 30, 2008, in forum: C++
    Replies:
    1
    Views:
    759
  4. Boris Du¹ek
    Replies:
    3
    Views:
    1,443
    Boris Du¹ek
    Nov 3, 2008
  5. xmllmx
    Replies:
    5
    Views:
    470
    James Kanze
    Nov 5, 2010
Loading...

Share This Page