C ++复位区域设置为“C”全球? [英] C++ reset locale to "C" globally?

查看:173
本文介绍了C ++复位区域设置为“C”全球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前正在开发的项目中,我链接到一个专有的动态库。一旦我运行库的初始化函数,记录和打印数字的行为就会改变。



每三度小数点插入一个逗号。

  cout< 123456789<< endl 

用于打印 123456789 它打印 123,456,789 。这是非常讨厌的,因为这种行为不是我想要的。



这个问题不仅是明显的二进制我正在编译,但也显示在所有 couts stringstreams



我已经尝试在调用初始化函数后使用这行代码

  setlocale(LC_ALL,C); 

认为它可能将我的区域设置重置为默认值;但无济于事。逗号持续存在!



此代码片段:

  std :: cout.imbue(std :: locale(C)); 

可用于重置 couts 每个 stringstream 我也应用它。但是,我真的需要调用 imbue 在每个链接到的每个库中声明的EVERY stringstream 有一些库是专有的,我不能实际更改其源代码。



必须有一种方法将区域设置重置为C c> std :: locale :: global c> (std :: locale(C)); 应该做的伎俩。请参见 http://en.cppreference.com/w/cpp/locale/locale/全球



请注意,这仅影响 之后创建的流。



其他库已经嵌入的任何流,例如 cout 都必须重新填充回所需的默认语言环境。



最后,我强烈建议针对你使用的库提交一个缺陷报告,因为在你的初始化函数中单方面做出如此明显的全局变化是很不合理的。


In a project I am currently working on I link to a proprietary dynamic library. As soon as I run the library's initialize function, the behavior of logging and printing of numbers changes.

Commas have been inserted at every third decimal. Ie.

cout << 123456789 << endl

used to print out 123456789 and now it prints 123,456,789. This is horribly annoying, because this behavior is not what I want.

This issue is not only apparent in the binary I am compiling, but also shows up in all the couts and stringstreams in the libraries that I link to it.

I have tried using this line of code after calling the initialize function

setlocale(LC_ALL,"C");

thinking it might reset my locale to the default; but to no avail. The commas persist!!

This snippet of code:

std::cout.imbue(std::locale("C"));

works to reset the locale of couts and every stringstream I apply it too. However, do I really need to call imbue on EVERY stringstream declared in EVERY library I link to? There are some libraries that are proprietary and I cannot actually change their source code.

There must be a way to reset the locale back to "C" globally?

解决方案

I believe std::locale::global(std::locale("C")); should do the trick. See http://en.cppreference.com/w/cpp/locale/locale/global

Note that this only affects streams created after this call.

Any streams such as cout that the other library already imbued would have to be re-imbued back to the desired default locale.

And finally I would strongly suggest filing a defect report against the library you're using because it's pretty unjustifiable to to unilaterally make such striking global changes within your initialization function.

这篇关于C ++复位区域设置为“C”全球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆