The C Runtime locale is set by setlocale
.
The Standard C++ Library (STL) locale is set by the std::locale
class and can be set on individual STL objects like std::istringstream etc.
The function _configthreadlocale(_ENABLE_PER_THREAD_LOCALE)
allows setting the C Runtime locale on a per thread basis.
Unfortunately it seems that STL objects in threads where _configthreadlocale(_ENABLE_PER_THREAD_LOCALE)
is enabled is using the C Runtime locale. Or at least using the decimal point of the C Runtime locale.
In threads without _ENABLE_PER_THREAD_LOCALE
there are no problems.
Something similar was asked by Paavo in 2008, but with no answers: _configthreadlocale and localeconv
The following code shows the problem:
//Enable per thread locale in current thread
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)
//Create istringstream object
std::istringstream LibraryStream;
//Create double object
double Value = 0;
//Create std::locale object with "C" locale ("." as decimal point)
std::locale StreamLoc("C");
//Set C Runtime locale to danish ("," as decimal point)
setlocale(LC_ALL, "danish");
//Set the "C" locale on the istringstream object
LibraryStream.imbue(StreamLoc);
//Get the locale of the istringstream object for test (returns "C" as expected)
std::locale NewStreamLoc = LibraryStream.getloc();
//Set floating point string with "C" locale decimal point in istringstream object
LibraryStream.str("60.258351");
//Convert the string to double
LibraryStream >> Value;
//Now the expected value of "Value" is 60.258351, but it is 60.000
//when debugging the conversion, I can see that "," is used as decimal point
Have anyone experienced this before? Am I doing something wrong? Are there any suggestions for solutions?
Thanks in advance /TEB