简体   繁体   English

locale::facet::_S_create_c_locale 名称对空本地名称无效

[英]locale::facet::_S_create_c_locale name not valid for empty local name

I was writing a function for printing u8string to ostream .我正在写一个 function 用于将u8string打印到ostream This is what I came up with:这就是我想出的:

auto operator<<(std::ostream& out, const std::u8string_view str) -> std::ostream& {
    std::locale::global(std::locale{".utf8"});
    auto& ret = out << std::string_view{std::bit_cast<const char*>(str.data()), str.size()};
    std::locale::global(std::locale{""});
    return ret;
}

This compiles fine.这编译得很好。 But while executing, this fails saying:但是在执行时,这失败了:

terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid

First I thought ".utf8" locale might not be supported on my system (that's true).首先,我认为我的系统可能不支持".utf8"语言环境(这是真的)。 But even after I comment out that line, I'm still getting the same error.但即使在我注释掉该行之后,我仍然遇到同样的错误。 So the error must have been caused by using "" .所以错误一定是使用""引起的。

I always thought an empty locale name sets the locale to the user's preference.我一直认为空的语言环境名称会将语言环境设置为用户的首选项。 If this is not happening, how should I restore the user-preferred locale after writing to the ostream ?如果这没有发生,我应该如何在写入ostream后恢复用户首选的语言环境?

I am using g++ 12.2.0 Rev6, built by MSYS2 project on Windows 11 x64 .我正在使用g++ 12.2.0 Rev6,由Windows 11 x64上的 MSYS2 项目构建。

std::locale::global returns the previous locale, so you can use that to restore it: std::locale::global返回以前的语言环境,因此您可以使用它来恢复它:

auto operator<<(std::ostream& out, const std::u8string_view str) -> std::ostream& {
    auto locale = std::locale::global(std::locale{".utf8"});
    auto& ret = out << std::string_view{std::bit_cast<const char*>(str.data()), str.size()};
    std::locale::global(locale);
    return ret;
}

But why are you messing with the locale in the first place?但是你为什么首先要搞乱语言环境? Writing chars to an ostream is not affected by the locale.将字符写入ostream不受语言环境的影响。 And when you actually do something with the stream that is affected by locale, you should change the stream's locale, not the global locale:当您实际对受区域设置影响的 stream 执行某些操作时,您应该更改流的区域设置,而不是全局区域设置:

my_stream.imbue(std::locale{"en_US.utf8"})

PS: that is not a sensible use of bit_cast , you should use reinterpret_cast . PS:这不是bit_cast的明智使用,您应该使用reinterpret_cast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 了解语言环境类,locale :: facet :: _ S_create_c_locale名称无效 - Understanding locale class, locale::facet::_S_create_c_locale name not valid C ++:服务器上的boost :: filesystem问题(locale :: facet :: _ S_create_c_locale名称无效) - C++: Issues with boost::filesystem on server (locale::facet::_S_create_c_locale name not valid) 抛出 &#39;std::runtime_error&#39; 的实例 what(): locale::facet::_S_create_c_locale name not valid - throwing an instance of 'std::runtime_error' what(): locale::facet::_S_create_c_locale name not valid 从命令行运行程序时C ++错误“失败:locale :: facet :: _ S_create_c_locale名称无效” - C++ Error “failure: locale::facet::_S_create_c_locale name not valid” when running program from command line C ++-运行Moses解码器时语言环境名称无效异常 - C++ - locale name not valid exception when running Moses decoder 韩语的语言环境名称是什么? - What is the locale name for Korean? combine 不会更改语言环境名称 - combine does not change the locale name 获取boost :: locale :: conv中函数的用户代码页名称 - Get the user's codepage name for functions in boost::locale::conv 语言环境Facet构造函数被忽略 - locale Facet Constructor Ignored 拥有/删除区域设置中的构面(std :: locale) - Ownership/delete'ing the facet in a locale (std::locale)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM