6

我试图在逗号是小数点分隔符的德语语言环境中一起使用 boost::locale 和 std::stod 。考虑这段代码:

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("") 创建正确的语言环境,输出为

d1: 1,1
d2: 2,2

正如我所料。当我注释掉第 (1) 行并取消注释第 (2) 行时,输出为

d1: 1
d2: 2.2

d2 的结果是意料之中的。据我了解 boost::locale 希望我明确指定 d2 应格式化为数字并执行

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

再次将输出固定为 2,2。问题是 std::stod 不再将 1,1 视为有效的浮点数并将其截断为 1。

我的问题是:为什么当我使用 boost::locale 生成我的语言环境时 std::stod 停止工作?

附加信息:我使用的是 VC++2015,Boost 1.60,没有 ICU,Windows 10

更新:

我注意到,当我两次设置全局语言环境时,问题已得到解决,首先是 std::locale(""),然后是 boost:

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

不过,我不知道它为什么会这样!

4

1 回答 1

6

长话短说: boost::locale仅更改全局 c++-locale 对象,但不更改 C-locale。 stod使用 C-locale 而不是全局 c++-locale 对象。std::locale更改:全局 c++ 语言环境对象和 C 语言环境。


整个故事: std::locale是一个微妙的事情,负责很多调试!

让我们从 c++ 类 std::locale 开始:

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

创建德语语言环境(如果它在您的机器上可用,否则会抛出),这会在de_DE.utf8控制台上产生。

但是,它不会更改全局c++ 语言环境对象,该对象是在程序启动时创建的并且是经典的(“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 时,它会从全局 c++ 状态中克隆其语言环境(这里我们使用字符串流,但它相同)。全局状态的后续更改不会影响流:

 //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。正如您可以想象的那样,它使用全局 c++ 语言环境(不完全正确,请耐心等待)状态,而不是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++ 语言环境时,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。但是现在全局 c++ 语言环境是什么?

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

如您所料,C 对 c++ 全局语言环境一无所知,并且保持不变:global c++ state: C.

现在我们不在堪萨斯了!旧的 c 函数将使用 C 语言环境,而新的 c++ 函数将使用全局 c++。为有趣的调试做好准备!

你会期待什么

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

去做?std::stod毕竟是一个全新的 c++11 函数,它应该使用全局 c++ 语言环境!再想想...:

1 vs. 1.1

它获得了正确的德语格式,因为 C 语言环境设置为“de_DE.utf8”,并且它在后台使用旧的 C 样式函数。

只是为了完整起见,std::streams使用全局 c++ 语言环境:

  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 可能不是最快的,但有其优点......

于 2016-01-13T08:40:57.963 回答