Skip to content

<locale>: std::locale alters the global C-locale #5785

@dalle

Description

@dalle

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 '.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions