Created attachment 31489 [details] g++ -v (Actually, it is set, it's just cleared before the return to caller) The problem as I understand it is that the call to __is.putback(__ch) resets the eofbit to 0, (in C++11) after a valid eof has been detected. The next extraction operation fails on attempting to get a char type but the eofbit is not set to 1. This precludes the ability to do error checking on input files. A simple solution I think would be to test for the eofbit before attempting the putback. ie: else { if (! __is.eof()) { __is.putback(__ch); __is >> __re_x; __x = __re_x; } } return __is; } in file /usr/include/c++/4.8.1/complex (This only fixes int types)
Created attachment 31490 [details] test program
Created attachment 31491 [details] input test file
Created attachment 31492 [details] output
Created attachment 31493 [details] make command
Reduced: #include <sstream> #include <complex> #include <assert.h> int main () { std::istringstream in; std::complex<double> c; in >> c; assert(in.fail()); assert(in.eof()); }
The simplest fix is just: --- a/libstdc++-v3/include/std/complex +++ b/libstdc++-v3/include/std/complex @@ -495,6 +495,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Tp __re_x, __im_x; _CharT __ch; __is >> __ch; + if (!__is) + return __is; if (__ch == '(') { __is >> __re_x >> __ch; i.e. don't use putback if we didn't extract a character, so don't clear the eofbit unless there's actually something to putback. But the current implementation has other problems, like not doing a putback for the character extracted when a ',' or ')' is not found. This is closely related to https://wg21.link/lwg2714
Author: redi Date: Wed Dec 13 17:02:14 2017 New Revision: 255608 URL: https://gcc.gnu.org/viewcvs?rev=255608&root=gcc&view=rev Log: PR libstdc++/59568 fix error handling for std::complex stream extraction PR libstdc++/59568 * include/std/complex (operator>>): Implement proposed resolution to LWG 2714. Use putback if and only if a character has been successfully extracted but isn't a delimiter. Use ctype::widen and traits::eq when testing if extracted characters match delimiters. * testsuite/26_numerics/complex/dr2714.cc: New test. Added: trunk/libstdc++-v3/testsuite/26_numerics/complex/dr2714.cc Modified: trunk/libstdc++-v3/ChangeLog trunk/libstdc++-v3/include/std/complex
Fixed for GCC 8.
Author: redi Date: Thu Dec 14 11:53:02 2017 New Revision: 255633 URL: https://gcc.gnu.org/viewcvs?rev=255633&root=gcc&view=rev Log: PR libstdc++/59568 don't use putback or update value when extraction fails PR libstdc++/59568 * include/std/complex (operator>>): Only use putback if a character was successfully extracted and only set the value if a number was successfully extracted. * testsuite/26_numerics/complex/inserters_extractors/char/59568.cc: New test. Added: branches/gcc-7-branch/libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/char/59568.cc Modified: branches/gcc-7-branch/libstdc++-v3/ChangeLog branches/gcc-7-branch/libstdc++-v3/include/std/complex
Author: redi Date: Thu Dec 14 12:01:40 2017 New Revision: 255634 URL: https://gcc.gnu.org/viewcvs?rev=255634&root=gcc&view=rev Log: PR libstdc++/59568 don't use putback or update value when extraction fails PR libstdc++/59568 * include/std/complex (operator>>): Only use putback if a character was successfully extracted and only set the value if a number was successfully extracted. * testsuite/26_numerics/complex/inserters_extractors/char/59568.cc: New test. Added: branches/gcc-6-branch/libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/char/59568.cc Modified: branches/gcc-6-branch/libstdc++-v3/ChangeLog branches/gcc-6-branch/libstdc++-v3/include/std/complex
Also fixed for 6.5 and 7.3