Stod does not work correctly with boost :: locale

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

boost::locale::generator gen;

std::locale loc("");  // (1)
//std::locale  loc = gen("");  // (2)

std::locale::global(loc);
std::cout.imbue(loc);

std::string s = "1,1";  //float string in german locale!
double d1 = std::stod(s);
std::cout << "d1: " << d1 << std::endl;

double d2 = 2.2;
std::cout << "d2: " << d2 << std::endl;

std :: locale loc ("") creates the correct locale, and the output is

d1: 1,1
d2: 2,2

as i expect. When I comment out line (1) and line uncomment (2), the output

d1: 1
d2: 2.2

Expected result for d2. As far as I understand, boost :: locale wants me to explicitly indicate that d2 should be formed as a number and do

std::cout << "d2: " << boost::locale::as::number << d2 << std::endl;

corrects the output again at 2.2. The problem is that std :: stod no longer treats the value 1.1 as a valid floating point number and truncates it to 1.

My question is: why does std :: stod stop working when I create my language with boost :: locale?

Additional information: I am using VC ++ 2015, Boost 1.60, not ICU, Windows 10

Update:

, , , std:: locale (""), boost:

std::locale::global(std::locale(""));
bl::generator gen;
std::locale::global(gen(""));

, , !

+4
1

: boost::locale ++ - locale, C-locale. stod C-, ++ - locale. std::locale : ++ - locale, C.


: std::locale - !

++ std:: locale:

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

( , ), de_DE.utf8 .

global ++, ( "C" ). std::locale :

...
  std::locale loc2;
  std::cout<<loc2.name()<<"\n\n\n";

C, . std:: locale ("") , , .

std::local::global:

  std::locale::global(loc);
  std::locale loc3;
  std::cout<<loc3.name()<<"\n\n\n";

de_DE.utf8 . , :

  std::locale::global(std::locale::classic());
  std::locale loc4;
  std::cout<<loc4.name()<<"\n\n\n";

C.

, std:: cout , ++ ( , ). :

 //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<<c_stream.str()<<" vs. "<<de_stream.str()<<"\n";

1.1 vs. 1,1 -

- imbue(std::locale::classic()), , :

  de_stream.imbue(std::locale::classic());
  de_stream<<" vs. "<<1.1;
  std::cout<<de_stream.str()<<"\n";
  std::cout<<"global c++ state: "<<std::locale().name()<<"\n";

:

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

std::stod. , ++ locale ( , ) , () cout -stream:

std::cout<<std::stod("1.1")<<" vs. "<<std::stod("1,1")<<"\n";

1 vs. 1.1, "de_DE.utf8", '.', std::cout - "C". :

  std::locale::global(std::locale::classic());
  std::cout<<std::stod("1.1")<<" vs. "<<std::stod("1,1")<<"\n";

"1,1" : 1.1 vs. 1

, , - std::stod.

++ () C ( C C). , ++, C .

/ C std::setlocale(...). :

std::cout<<"(global) C locale is "<<std::setlocale(LC_ALL,NULL)<<"\n";

(global) C locale is C. C:

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

(global) C locale is de_DE.utf8. ++?

std::cout<<"global c++ state: "<<std::locale().name()<<"\n";

, C ++ : global c++ state: C.

! c- C-locale ++- global ++. !

std::cout<<"C: "<<std::stod("1.1")<<" vs. DE :"<<std::stod("1,1")<<"\n";

? std::stod - ++ 11, ++! :

1 vs. 1.1

, C-locale 'de_DE.utf8', C .

, std::streams ++:

  std::stringstream stream;//creating with global c++ locale
  stream<<1.1;
  std::cout<<"I'm still in 'C' format: "<<stream.str()<<"\n";

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

: :

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
}

:

  double d=0;
  std::cout<<"1,1 parsed with German locale successfully :"<<s2d("1,1", d, std::locale("de_DE.utf8"))<<"\n";
  std::cout<<"value retrieved: "<<d<<"\n\n";

  d=0;
  std::cout<<"1,1 parsed with Classical locale successfully :"<<s2d("1,1", d, std::locale::classic())<<"\n";
  std::cout<<"value retrieved: "<<d<<"\n\n";

  d=0;
  std::cout<<"1.1 parsed with German locale successfully :"<<s2d("1.1", d, std::locale("de_DE.utf8"))<<"\n";
  std::cout<<"value retrieved: "<<d<<"\n\n";

  d=0;
  std::cout<<"1.1 parsed with Classical locale successfully :"<<s2d("1.1", d, std::locale::classic())<<"\n";
  std::cout<<"value retrieved: "<<d<<"\n\n";

:

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 , ...

+3

Source: https://habr.com/ru/post/1623932/


All Articles