You should catch exceptions as described in
Locales to prevent your app from crashing.
#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).