[libcxx-dev] Different behavior from stringstream::operator>>() in libc++ vs libstdc++
Ryan S. Elliott via libcxx-dev
libcxx-dev at lists.llvm.org
Tue Jan 26 12:18:20 PST 2021
Hello,
I've encountered inconsistent behavior in this case and I'm wondering if this
is a bug (either in libc++ or libstdc++) or is a situation where the c++
standard allows for implementation dependent behavior? Any comments would be
appreciated! Thanks.
The attached code "read-double.cpp" contains:
```
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::stringstream f("1.30429295CCC");
double a;
std::string b;
if (!(f >> a).fail())
std::cout << "Read double a as " << a << std::endl;
else
std::cout << "unable to read double a" << std::endl;
f.clear();
if (!(f >> b).fail())
std::cout << "Read string b as " << b << std::endl;
else
std::cout << "unable to read string b" << std::endl;
}
```
When compiled with g++ it outputs:
% g++ read-double.cpp
% ./a.out
Read double a as 1.30429
Read string b as CCC
When compiled with clang++ it outputs:
% clang++ read-double.cpp
% ./a.out
unable to read double a
unable to read string b
These tests were done on this machine (uname -a):
Darwin ryans-mbp-6.lan 19.6.0 Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45
PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64 x86_64
with this g++ (g++ --version)
g++-10 (Homebrew GCC 10.2.0_2) 10.2.0
and this clang++ (clang++ --version)
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
-------------- next part --------------
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::stringstream f("1.30429295CCC");
double a;
std::string b;
if (!(f >> a).fail())
std::cout << "Read double a as " << a << std::endl;
else
std::cout << "unable to read double a" << std::endl;
f.clear();
if (!(f >> b).fail())
std::cout << "Read string b as " << b << std::endl;
else
std::cout << "unable to read string b" << std::endl;
}
More information about the libcxx-dev
mailing list