After configuring a std::istringstream
to throw exceptions when failbit
is set I get no exceptions happening with libc++ (this is under linux with libc++ compiled with support from libcxxrt). I suppose this is a bug in libc++ or libcxxrt:
#include <iostream>
#include <sstream>
template<typename T> std::istream &getvalue(std::istream &is, T &value, const T &default_value = T())
{
std::stringstream ss;
std::string s;
std::getline(is, s, ',');
ss << s;
if((ss >> value).fail())
value = default_value;
return is;
}
int main()
{
std::string s = "123,456,789";
std::istringstream is(s);
unsigned n;
try
{
is.exceptions(std::ios::failbit | std::ios::eofbit);
getvalue(is, n);
std::cout << n << std::endl;
getvalue(is, n);
std::cout << n << std::endl;
// Disable EOF exception on last bit
is.exceptions(std::ios::failbit);
getvalue(is, n);
std::cout << n << std::endl;
// Force Fail reading after EOF
getvalue(is, n);
std::cout << n << std::endl;
}
catch(std::ios::failure &fail)
{
std::cout << "Fail" << std::endl;
}
}
output for libstdc++:
123
456
789
Fail
libc++/libcxxrt output:
123
456
789
0
EDIT
Also tested on OS X.
Bug submitted: http://llvm.org/bugs/show_bug.cgi?id=15949