Brian Inglis via Cygwin wrote:
On 2023-09-21 10:28, Takashi Yano via Cygwin wrote:
On Fri, 22 Sep 2023 01:12:04 +0900
Takashi Yano wrote:
I wonder why the following code throws std::runtime_error
even though the LC_ALL is set to valid locale other than "C".
This does not occur only when LC_ALL is set to "C".

#include <locale>
int main()
{
    std::locale("");
    return 0;
}

In linux, this occurs only when the LC_ALL is set to invalid
locale (i.e. locale that is not registered in system).

Similarly,
std::locale("ja_JP.UTF-8")
throws std::runtime_error in cygwin.

Looks like the implementation does not like any default "" or explicit "en_US.UTF-8" strings there! See example at link and below; results are always the same:

    https://en.cppreference.com/w/cpp/locale/locale

#include <iostream>
#include <locale>

int main()
{
    std::wcout << "User-preferred locale setting is "
           << std::locale().name().c_str() << '\n';

    // on startup, the global locale is the "C" locale
    std::wcout << 1000.01 << '\n';

    // replace the C++ global locale and the "C" locale with the user-preferred locale
    std::locale::global(std::locale(""));
    // use the new global locale for future wide character output
    std::wcout.imbue(std::locale());

    // output the same number again
    std::wcout << 1000.01 << '\n';
}

$ g++ -o c++locale{,.cc}
$ ./c++locale
User-preferred locale setting is C
1000.01
terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid
Aborted (core dumped)


According to libstdc++ source, the internal function locale::facet::_S_create_c_locale() calls some __newlocale() which apparently does not arrive at newlocale() from cygwin1.dll. But cygstdc++-6.dll imports newlocale() from cygwin1.dll.

Only standard locale "C" and its alias "POSIX" work with C++ std::locale().

The cygwin1.dll function newlocale() works as expected - except that it does not set errno if the locale name is invalid.

--
Regards,
Christian


--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to