Praying
Praying

Reputation: 71

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

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

Upvotes: 5

Views: 235

Answers (1)

xskxzr
xskxzr

Reputation: 13040

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.

Upvotes: 3

Related Questions