为什么当我将 std::locale 设置为 "zh_CN.UTF-8" 时 std::istringstream 失败?

why std::istringstream fail when I set std::locale to "zh_CN.UTF-8"?

本文关键字:std CN UTF-8 失败 istringstream locale 为什么 设置 zh      更新时间:2023-10-16

代码如下:

#include <iostream>
#include <locale>
#include <sstream>
int main() 
{
    std::locale::global(std::locale("zh_CN.UTF-8"));
    std::string str = u8"8086";
    std::istringstream iss(str);
    int e;
    iss >> e;
    if (iss.fail())
    {
        std::cout<<"failed  "<<"e = "<<e<<std::endl;
    }
    return 0;
}

输出为:

failed  e = 8086

operator>>是成功的,但为什么fail()返回为真?


我在 centos7 中尝试过,fail()返回false,但是当我在 macOS 中运行时,fail()返回true?为什么?

----------------------环境-------------------

Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
这是一个

libc++错误。它强制检查分组字符的位置,即使数字中没有分组字符。

现在,您可以添加分组字符来解决此问题,即改用"8,086"