stod does not work correctly with boost::locale

前端 未结 1 1018
抹茶落季
抹茶落季 2021-01-13 07:41

I am trying to use boost::locale and std::stod together in a german locale where a comma is the decimal separator. Consider this code:

boost::locale::generat         


        
1条回答
  •  孤街浪徒
    2021-01-13 08:05

    Long story short: boost::locale changes only the global c++-locale object, but not the C-locale. stod uses the C-locale and not the global c++-locale object. std::localechanges both: the global c++-locale object and the C locale.


    The whole story: std::locale is a subtle thing and responsible for a lot of debugging!

    Let's start with the c++ class std::locale:

      std::locale loc("de_DE.utf8");  
      std::cout<

    creates the German locale (if it is available on your machine, otherwise it throws), which results in de_DE.utf8 on the console.

    However it does not change the global c++ locale object, which is created at the start-up of your program and is classical ("C"). The constructor of std::locale without arguments returns a copy of the global state:

    ...
      std::locale loc2;
      std::cout<

    Now you should see C if nothing messed up your locale before. std::locale("") would do some magic and find out the preferences of the user and return it as object, without changing the global state.

    You can change the local state with std::local::global:

      std::locale::global(loc);
      std::locale loc3;
      std::cout<

    The default constructor results this time in de_DE.utf8 on the console. We can restore the global state to the classical by calling:

      std::locale::global(std::locale::classic());
      std::locale loc4;
      std::cout<

    which should give you C again.

    Now, when the std::cout is created it clones its locale from the global c++ state (here we do it with the stringstreams, but it the same). Later changes of the global state does not affect the stream:

     //classical formating
      std::stringstream c_stream;
    
     //german formating:
      std::locale::global(std::locale("de_DE.utf8"));
      std::stringstream de_stream;
    
      //same global locale, different results:
      c_stream<<1.1;
      de_stream<<1.1;
    
      std::cout<

    Gives you 1.1 vs. 1,1 - the first is the classical the second german

    You can change the local locale-object of a stream with imbue(std::locale::classic()) it goes without saying, that this doesn't change the global state:

      de_stream.imbue(std::locale::classic());
      de_stream<<" vs. "<<1.1;
      std::cout<

    and you see:

    1,1 vs. 1.1
    global c++ state: de_DE.utf8
    

    Now we are coming to std::stod. As you can imagine it uses the global c++ locale (not entirely true, bear with me) state and not the (private) state of the cout-stream:

    std::cout<

    gives you 1 vs. 1.1 because the global state is still "de_DE.utf8", so the first parsing stops at '.' but the local state of std::cout is still "C". After restoring the global state we get the classical behaviour:

      std::locale::global(std::locale::classic());
      std::cout<

    Now the German "1,1" is not parsed properly: 1.1 vs. 1

    Now you might think we are done, but there is more - I promised to tell you about std::stod.

    Next to the global c++ locale there is so called (global) C locale (comes from the C language and not to be confused with the classical "C" locale). Each time we changed the global c++ locale the C locale has been changed too.

    Getting/setting of the C locale can be done with std::setlocale(...). To query the current value run:

    std::cout<<"(global) C locale is "<

    to see (global) C locale is C.To set the C locale run:

      assert(std::setlocale(LC_ALL,"de_DE.utf8")!=NULL);
      std::cout<<"(global) C locale is "<

    which yields (global) C locale is de_DE.utf8. But what is now the global c++ locale?

    std::cout<<"global c++ state: "<

    As you may expect, C knows nothing about c++ global locale and leaves it unchanged: global c++ state: C.

    Now we are not in Kansas any more! The old c-functions would use the C-locale and new c++ function the global c++. Brace yourself for funny debugging!

    What would you expect

    std::cout<<"C: "<

    to do? std::stod is a brand-new c++11 function after all and it should use global c++ locale! Think again...:

    1 vs. 1.1
    

    It gets the German format right, because the C-locale is set to 'de_DE.utf8' and it uses old C-style functions under the hood.

    Just for the sake of completeness, the std::streams use the global c++ locale:

      std::stringstream stream;//creating with global c++ locale
      stream<<1.1;
      std::cout<<"I'm still in 'C' format: "<

    gives you: I'm still in 'C' format: 1.1.

    Edit: An alternative method to parse string without messing with global locale or be disturbed by it:

    bool s2d(const std::string &str, double  &val, const std::locale &loc=std::locale::classic()){
    
      std::stringstream ss(str);
      ss.imbue(loc);
      ss>>val;
      return ss.eof() && //all characters interpreted
             !ss.fail(); //nothing went wrong
    }
    

    The following tests shows:

      double d=0;
      std::cout<<"1,1 parsed with German locale successfully :"<

    That only the first and the last conversions are successful:

    1,1 parsed with German locale successfully :1
    value retrieved: 1.1
    
    1,1 parsed with Classical locale successfully :0
    value retrieved: 1
    
    1.1 parsed with German locale successfully :0
    value retrieved: 11
    
    1.1 parsed with Classical locale successfully :1
    value retrieved: 1.1
    

    std::stringstream may be not the fastest but has its merits...

    0 讨论(0)
提交回复
热议问题