Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an std::locale with an empty string ""

Tags:

c++

std

locale

Question

Is constructing std::locale with an empty string to get the user-preferred native locale a part of the standard? If yes, could you point out a source which explicitly states that?

Problem description

Example from documentation of std::locale has this line:

std::wcout << "User-preferred locale setting is " << std::locale("").name().c_str()

Which hints that creating a locale with an empty string will return a user-preferred native locale. After quick googling, this article also mentions:

The empty string tells setlocale to use the locale specified by the user in the environment.

However, when looking at the documentation for std::locale constructors, there is no mentioning of a special case, when an empty string is provided.

Here's the quote:

3-4) Constructs a copy of the system locale with specified std_name (such as "C", or "POSIX", or "en_US.UTF-8", or "English_US.1251"), if such locale is supported by the operating system. The locale constructed in this manner has a name.

like image 417
AMA Avatar asked Sep 22 '17 12:09

AMA


1 Answers

The draft standard says in [locale.cons]:

explicit locale(const char* std_name);
  1. Effects: Constructs a locale using standard C locale names, e.g., "POSIX". The resulting locale implements semantics defined to be associated with that name.

  2. Throws: runtime_error if the argument is not valid, or is null.

  3. Remarks: The set of valid string argument values is "C" , "" , and any implementation-defined values.

This says "" is a valid constructor argument, and arguments are standard C locale names.

Then in [c.locale] it explicitly refers to the standard C header <locale.h>.

Quoting from the C standard (C99), 7.11.1.1/3:

A value of "C" for locale specifies the minimal environment for C translation; a value of "" for locale specifies the locale-specific native environment. Other implementation-defined strings may be passed as the second argument to setlocale.

I think this means the answer to your question is "yes": A name of "" refers to the native locale.

like image 152
melpomene Avatar answered Oct 14 '22 13:10

melpomene