为什么当我将 std::locale 设置为 "zh_CN.UTF-8" 时 std::istringstream 失败?
why std::istringstream fail when I set std::locale to "zh_CN.UTF-8"?
代码如下:
#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"
。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 使用一个考虑到std::map中键值的滚动或换行的键
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么 std::unique 不调用 std::sort?
- 使用std::函数映射对象方法
- 可组合的lambda/std::函数与std::可选