Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ reset locale to "C" globally?

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?

like image 655
dinkelk Avatar asked Jun 19 '13 23:06

dinkelk


1 Answers

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.

like image 152
Mark B Avatar answered Sep 18 '22 23:09

Mark B