Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

the code is as follow:

#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 output is:

failed  e = 8086

The operator>> is successful but why does fail() return true?


I tried it in centos7 and fail() returned false, but when I run it in 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
like image 896
Praying Avatar asked Oct 17 '22 23:10

Praying


1 Answers

This is a libc++ bug. It enforces to check the position of grouping characters even if there is no grouping character in the number.

For now, you can add grouping characters to fix this issue, i.e. use "8,086" instead.

like image 114
xskxzr Avatar answered Nov 15 '22 06:11

xskxzr