提问人:David Lehavi 提问时间:10/4/2023 更新时间:10/4/2023 访问量:80
从 g++/stdlib++ 和 clang++/libc++ 之间的流中读取双精度的区别
Difference reading a double from stream between g++/stdlib++ and clang++/libc++
问:
最小的“失败”示例:
#include <assert.h>
#include <iostream>
#include <string>
#include <sstream>
int main(int argv, char** argc) {
std::string line = "1.1", line2 = "1.1X";
std::istringstream iss(line), iss2(line2);
double x;
assert(iss >> x);
std::cout << x << "\n" << std::flush;
assert(iss2 >> x);
std::cout << x << "\n";
return 0;
}
使用 clang 运行:
>clang++ -stdlib=libc++ reading_double.cc -o reading_double
>./reading_double
1.1
reading_double: reading_double.cc:11: int main(int, char **): Assertion `iss2 >> x' failed.
Aborted (core dumped)
>clang --version
Ubuntu clang version 18.0.0 (++20231004042239+548d67a0393c-1~exp1~20231004042400.1224)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
使用 gcc 运行:
>g++ reading_double.cc -o reading_double
>./reading_double
1.1
1.1
>g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
哪个是正确的行为?(在我看来,libc++ 是错误的)
答:
0赞
David Lehavi
10/4/2023
#1
根据评论中与@Bob_的讨论。 这是 libc++ 中的已知错误 我发现的关于这个错误的最深入的讨论是在
https://github.com/tardate/LittleCodingKata/blob/main/cpp/DoubleTrouble/README.md
评论
double
iss2
'X'