Exception problem with locale with g++ on Windows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi everyone,

the following program should switch to the current locale and then print
the name of that locale:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <locale>
#include <iostream>

int main()
{
  std::locale::global(std::locale(""));

  std::locale current_locale;
  std::cout << "Locale is now: " << current_locale.name() << std::endl;
  return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It appears, however, that it doesn't do that on my 32-bits Windows 7.
The program compiles fine, but when I execute it, I get this (after
the usual "application has requested the runtime yadda yadda"):

    terminate called after throwing an instance of 'std::runtime_error'
      what(): locale::facet::_S_create_c_locale name not valid

(That is, an std::runtime_error exception is thrown by the constructor
of std::locale)

I used the locale name "" (empty string), which should refer to the
environment's locale and should be available on any platform (I even
think it is required to do that, but I don't have a C++ standard at
hand). Using the "C" locale works, but any other value causes the above
problem.

Compare to the identical C program:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
#include <locale.h>

int main()
{
  const char* str = setlocale(LC_ALL, "");
  printf("Locale set to %s\n", str);
  return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This program compiles and works as expected (on my system, it outputs
"Locale set to German_Germany.1252").

Some Internet research suggests that this problem has been seen by other
people as well, but I was not able to find a solution; in particular,
the solution to not adapt to the current user's locale is not acceptable
for a program intended to run under different locales.

Can someone please clarify what exactly is the problem with the C++
variant of this program, and how one can get it to work on Windows?

    $ g++ --version
    g++.exe (Rev1, Built by MSYS2 project) 6.3.0
    Copyright (C) 2016 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Greetings
Marvin

-- 
Blog: https://www.guelkerdev.de
PGP/GPG ID: F1D8799FBCC8BC4F



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux