代码如下:
#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()
会返回true?
我在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