#include #include #include int main(int argv, char** argc) { std::string " /> #include #include #include int main(int argv, char** argc) { std::string " /> #include #include #include int main(int argv, char** argc) { std::string "/>

Difference reading a double from stream between g++/stdlib++ and clang++/libc++

90 views Asked by At

minimal "failing" example:

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

Running with 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

Running with 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.

Which is the correct behavior ? (the way I see it, libc++ is wrong)

1

There are 1 answers

0
David Lehavi On

Per the discussion with @Bob_ in the comments. This is known bug in libc++ The most in depth discussion of this bug I found is at

https://github.com/tardate/LittleCodingKata/blob/main/cpp/DoubleTrouble/README.md