`<locale>`: std::locale alters the global C-locale
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 11.1k
- Forks
- 1.7k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 22
Description
Describe the bug
Constructing a std::locale with a different locale encoding than previously passed to setlocale also alters the global C-locale. This is especially obvious when having set an alternative decimal separator.
Command-line test case
PS C:\dev\test_locale> type test_locale.cpp
#include <locale>
#include <stdio.h>
#include <locale.h>
constexpr auto value = 1.1;
int main()
{
printf("setlocale: %s\n", setlocale(LC_ALL, ".utf8"));
puts("");
printf("decimal_point: %s\n", localeconv()->decimal_point);
printf("printf: %g\n", value);
puts("");
{
const auto loc = std::locale("");
}
printf("decimal_point: %s\n", localeconv()->decimal_point);
printf("printf: %g\n", value);
}
PS C:\dev\test_locale> cl /EHsc /W4 /WX /std:c++latest .\test_locale.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35217 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.
test_locale.cpp
Microsoft (R) Incremental Linker Version 14.44.35217.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test_locale.exe
test_locale.obj
PS C:\dev\test_locale> .\test_locale.exe
setlocale: English_United States.utf8
decimal_point: '
printf: 1'1
decimal_point: .
printf: 1.1
Expected behavior
I would expect std::locale to not alter the global C-locale, regardless of which encoding is used.
STL version
Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35217 for x86
Additional context
When running this program I have set the Time & language > Language & region > Regional format to "English (United States)", and I have altered the Regional format > Number format > Decimal symbol to a single quote '.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Windows command-line test case and inspect the behavior of std::locale("") alongside setlocale, localeconv, and printf. Trace the relevant STL locale implementation to determine why construction changes the global C-locale; done means preserving the configured decimal separator and matching the expected output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- localization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100