C運行時區域設置由setlocale
設置。 的標準C++庫(STL)區域設置由std::locale
類設置,並且可以在個體對象STL比如std :: istringstream等被設置當使用_ENABLE_PER_THREAD_LOCALE時,STL對象使用C運行時庫語言環境進行浮點轉換?
功能_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)
允許設置在每個線程的基礎上C運行區域設置。
不幸的是,啓用了_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)
的線程中的STL對象似乎正在使用C運行時區域設置。或者至少使用C運行時語言環境的小數點。
在沒有_ENABLE_PER_THREAD_LOCALE
的線程中沒有問題。類似
東西在2008年被要求帕沃,但沒有答案:_configthreadlocale and localeconv
下面的代碼顯示問題:
//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
有任何人經歷過這個?難道我做錯了什麼?有沒有解決方案的建議?
在此先感謝 /TEB
歡迎SO。在發佈代碼示例時,您可以使用編輯字段上方的小'101101'按鈕來正確獲取代碼格式。 – 2010-11-10 14:56:23
謝謝,我會記住的。 – TEB 2010-11-10 14:58:13