为什么将字符串流解析为double时失败?

时间:2018-10-28 02:30:26

标签: c++ stringstream libc++

我有以下代码:

#include <string>
#include <iostream>
#include <sstream>

int main()
{
        size_t x, y;
        double a = std::stod("1_", &x);
        double b = std::stod("1i", &y);

        std::cout << "a: " << a << ", x: " << x << std::endl;
        std::cout << "b: " << b << ", y: " << y << std::endl;

        std::stringstream s1("1_");
        std::stringstream s2("1i");

        s1 >> a;
        s2 >> b;

        std::cout << "a: " << a << ", fail: " << s1.fail() << std::endl;
        std::cout << "b: " << b << ", fail: " << s2.fail() << std::endl;
}

我想解析一个double并在遇到无效字符时停止。在这里,我尝试解析"1_""1i",它们都应该为我提供值1的双精度值。

这是我的输出:

a: 1, x: 1
b: 1, y: 1
a: 1, fail: 0
b: 0, fail: 1

因此stod函数按预期工作,但是stringstream方法却没有。对我来说,在标准库中使用两种解析双精度的标准方法会给出不同的结果对我来说没有意义?

为什么解析时stringstream方法失败"1i"

编辑:

这似乎给某些人带来不同的结果。我的编译器信息如下:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Edit2:

这是libc ++的错误,还是规范只是模糊了对double进行有效解析的计数?

1 个答案:

答案 0 :(得分:7)

这是libc++ bug。对于[facet.num.get.virtuals] bullet 3.2,仅当一个字符被允许作为在阶段1中确定的转换说明符的输入字段的下一个字符时才应累积(function dir_name($new_dir, $count){ $existing_dirs = array('dir', 'dir_1', 'dir_2'); if(in_array($new_dir, $existing_dirs)){ $count ++; if($count == 1) { $new_dir = $new_dir . '_' . $count; } if($count > 1){ $dir_parts = explode('_', $new_dir); $new_dir = $dir_parts[0] . '_' . $count; } dir_name($new_dir, $count); } else { //echo $new_dir; return $new_dir; } } echo dir_name('dir'); 对于%g)。累积了double之后,1被禁止,因此阶段2应该终止。

libc ++会不加选择地累积字符,直到达到非原子字符为止(它还将原子扩展为包括i,这是解析inf所必需的)。

相关问题