c++ - 使用空字符串 ""创建 std::locale

标签 c++ locale std

问题

是否使用空字符串构造 std::locale 以使用户首选的 native 语言环境成为标准的一部分?如果是,您能否指出明确说明这一点的来源?

问题描述

std::locale 文档中的示例有这一行:

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

这暗示使用空字符串创建语言环境将返回用户首选的本地语言环境。快速谷歌搜索后,这个 article还提到:

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

但是,在查看 documentation 时对于 std::locale 构造函数,没有提及提供空字符串时的特殊情况。

引用如下:

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.

最佳答案

draft standard[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.

这是说""是一个有效的构造函数参数,参数是标准的 C 语言环境名称。

然后在[c.locale] 中明确引用标准C 头文件<locale.h> .

引用自 C 标准 (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.

我认为这意味着您的问题的答案是"is":名称 ""指的是本地语言环境。

关于c++ - 使用空字符串 ""创建 std::locale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364608/

相关文章:

c++ - 检测用户离屏幕的距离

c++ - 将某些内容打印到 std::ostream 并返回 std::ostream 的函数?

C++ 仅在满足模板类型的条件时才创建类的实例

magento - 如何在开发 magento 扩展时使用本地化?

macos - cmake 不断添加 std=gnu++11 选项

c++ - 如何将 vector<T> 解析为模板函数的函数参数?

unix - zsh 中的 LANG 环境变量

vue.js - 如何在 Vue.js 中获得 fullCalendar 的完整翻译

c++ - std::vector operator[] 的不正确行为

c++ - 试图用 lambda 找到二维 vector 的最小元素