c++ - libc++ std::istringstream doesn't thrown exceptions. Bug? -


after configuring std::istringstream throw exceptions when failbit set no exceptions happening libc++ (this under linux libc++ compiled support libcxxrt). suppose 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 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

libc++ responding 27.7.2.1 [istream]/p4 describes basic_istream parse operator>> unsigned:

if 1 of these called functions throws exception, unless explicitly noted otherwise, input function sets badbit in error state. if badbit on in exceptions(), input function rethrows exception without completing actions, otherwise not throw , proceeds if called function had returned failure indication.

if:

is.exceptions(std::ios::failbit | std::ios::badbit); 

then desired behavior obtained.

123 456 789 fail 

update

chico rightly pointed out in comments below expected getline(is, s, ',') throw, not unsigned extractor.

looking @ 21.4.8.9 [string.io]/p7 describes getline:

effects: behaves unformatted input function (27.7.2.3), except not affect value returned subsequent calls basic_istream<>::gcount(). after constructing sentry object, if sentry converts true, calls str.erase() , extracts characters , appends them str if calling str.append(1, c) until of following occurs: ...

so question becomes:

how unformatted input function behave?

27.7.2.3 [istream.unformatted]/p1 says:

each unformatted input function begins execution constructing object of class sentry default argument noskipws (second) argument true. if sentry object returns true, when converted value of type bool, function endeavors obtain requested input. otherwise, if sentry constructor exits throwing exception or if sentry object returns false, when converted value of type bool, function returns without attempting obtain input. in either case number of extracted characters set 0; unformatted input functions taking character array of non-zero size argument shall store null character (using chart()) in first location of array. if exception thrown during input ios::badbit turned on315 in *this’s error state. (exceptions thrown basic_ios<>::clear() not caught or rethrown.) if (exceptions()&badbit) != 0 exception rethrown. counts number of characters extracted. if no exception has been thrown ends storing count in member object , returning value specified. in event sentry object destroyed before leaving unformatted input function.

315) done without causing ios::failure thrown.

(emphasis added me readability purposes)

so again appears indicate if exception desired parse operation, badbit has set in exceptions.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -