#include #incl...">

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

The code is as follows:

#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; } 

And the result:

 failed e = 8086 

operator>> succeeds, but why fail() returns true?


I tried this in centos7 and fail() returned false , but when I ran it on macOS, fail() returns true ? Why?

---------------------- Environment -------------------

 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 
+5
source share
1 answer

This is libC ++ error . It provides verification of the grouping position of characters, even if the number does not have a grouping character.

You can currently add grouping characters to fix this problem, i.e. use "8,086" instead.

+3
source

Source: https://habr.com/ru/post/1275156/


All Articles