teg*_*eye 5 c++ locale g++ setlocale clang++
在我的计算机(MacOS 10.14.x)上编译并运行以下代码会导致在clang ++上打印空字符串,并在g ++上引发运行时错误。为什么?
#include <locale>
#include <iostream>
int main()
{
std::cout << "User-preferred locale setting is " <<
std::locale("").name().c_str() << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ clang++ locale.cc
$ ./a.out
User-preferred locale setting is
$ g++-mp-8 locale.cc
$ ./a.out
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
User-preferred locale setting is Abort trap: 6
$ clang++ --version
clang version 7.0.1 (tags/RELEASE_701/final)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /opt/local/libexec/llvm-7.0/bin
$ g++-mp-8 --version
g++-mp-8 (MacPorts gcc8 8.3.0_0) 8.3.0
Copyright (C) 2018 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.
Run Code Online (Sandbox Code Playgroud)
目前,我认为这不是MacOS问题,因为在cppreference.com上运行示例也会产生不同的结果。
您可以在以下网址上针对不同的编译器版本自己尝试:https://en.cppreference.com/w/cpp/locale/locale/name
无论如何,它不会报告与以下内容相同的内容:
#include <locale>
#include <iostream>
#include <string>
int main()
{
std::cout << "User-preferred locale setting is "
<< setlocale(LC_ALL, "") << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于两个编译器,它返回相同的结果(“ en_US.UTF-8”)。
我想念什么?
你是对的“有效字符串参数值的集合是“C”,“”和任何实现定义的值......” 但是如果你尝试设置为未知的本地(可能由 local("" )) 它将引发运行时错误。
看一下源代码libstdc++-v3/config/locale/gnu/c_locale.cc
locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s, __c_locale __old)
{
__cloc = __newlocale(1 << LC_ALL, __s, __old);
if (!__cloc)
{
// This named locale is not supported by the underlying OS.
__throw_runtime_error(__N("locale::facet::_S_create_c_locale name not valid"));
}
}
Run Code Online (Sandbox Code Playgroud)
__newlocale
功能才是罪魁祸首。它是一个 C 函数,它将处理传递给它的值的转换。
在 MAC-OS 上,似乎libstdc++
没有正确处理“”值,甚至在许多语言环境中出现大量问题。
这是 libstdc++(g++ 使用的)中的一个众所周知的问题。您可以轻松地在多个地方找到它,错误报告 1、错误报告 2、 示例 1。如您所见,当前 libstdc++ 仅支持“C”语言环境。
我说使用ICU :)
归档时间: |
|
查看次数: |
118 次 |
最近记录: |