Author Topic: MinGW support for std::locale?  (Read 7546 times)

Offline BigAngryDog

  • Multiple posting newcomer
  • *
  • Posts: 75
    • BigAngryDog.com
MinGW support for std::locale?
« on: March 05, 2006, 06:47:40 am »
Am I correct in thinking the MinGW compiler (with Code::Blocks) does not support any locale other than the C locale?

I.e., the following raises an error:

Code
std::locale("en_US");

Sorry if this is numptie, question but I am new to MinGW & C::B.

Thanks
BigAngryDog.com

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: MinGW support for std::locale?
« Reply #1 on: March 05, 2006, 09:27:38 am »
You should catch exceptions as described in Locales to prevent your app from crashing.

Code
#include <iostream>
#include <stdexcept>
#include <locale>

int main()
{
    std::locale loc;
    try
    {
        loc = std::locale("en_US");
    }
    catch (std::runtime_error)
    {
        std::cerr << "locale not found\n";
        loc = std::locale("");       
    }
    return 0;
}
Should always work just fine. Empty string creates the default locale defined by the user's OS preferences (whatever that is). "en_US" like you used it should work too, if all is installed correctly (as it happens, it fails on my system,too - but that is not surprising, I don't have en_US).

In any case, always catch exceptions because locale will throw if anything goes wrong (as most things in std do).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline BigAngryDog

  • Multiple posting newcomer
  • *
  • Posts: 75
    • BigAngryDog.com
Re: MinGW support for std::locale?
« Reply #2 on: March 05, 2006, 02:31:31 pm »
Thanks for the reply thomas.

You say:

>"en_US" like you used it should work too, if all is installed correctly

That's interesting. How do I install a locale in my C::B/MinGW development environment?

Cheers
BigAngryDog.com

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: MinGW support for std::locale?
« Reply #3 on: March 05, 2006, 03:41:44 pm »
As far as I know, this is not a matter of MinGW installation, but of OS installation (since the standard library only supports the "C" locale and the "" locale itself). The other locales are provided by the OS. Don't take my word for it, though ;)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline BigAngryDog

  • Multiple posting newcomer
  • *
  • Posts: 75
    • BigAngryDog.com
Re: MinGW support for std::locale?
« Reply #4 on: March 05, 2006, 07:00:54 pm »
Out of interest, I note that Borland BCB not only allows me to set a range of std::locale("...") values, but they also do stuff, i.e. they change locale dependent behaviour. However, MinGW throws an exception if you try to set a locale to anything other than C classic.

There seems so much in the standard library with is "implementation specific"! Just my thought for the day. :)

Thanks for replying. Cheers.
BigAngryDog.com