Skip to content

<iostream>: Printing with std::cout changes the global locale permanently #5780

@lb90

Description

@lb90

Describe the bug

Using the streams provided by <iostream> can change the locale permanently

Command-line test case

D:\>type test-locale-stl.cpp
#include <iostream>
#include <clocale>

char *
query_locale_name()
{
  return std::setlocale(LC_ALL, nullptr);
}

int main()
{
  std::setlocale(LC_ALL, "norwegian-bokmal.UTF-8");

  std::cout << "locale before: " << query_locale_name() << std::endl;
  std::cout << 0.1 << std::endl;
  std::cout << "locale after: " << query_locale_name() << std::endl;
}

D:\>cl /EHsc /W4 /WX /std:c++latest .\test-locale-stl.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35217 for x64
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-stl.cpp
Microsoft (R) Incremental Linker Version 14.44.35217.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test-locale-stl.exe
test-locale-stl.obj

D:\>test-locale-stl
locale before: Norwegian Bokmål_Norway.utf8
0.1
locale after: C

Expected behavior

Locale after should be Norwegian Bokmål_Norway.utf8

STL version

Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35217 for x64

Additional context

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