https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65861
Bug ID: 65861 Summary: libstdc++ is silently generating wrong code when its std::search is given an input iterator Product: gcc Version: 5.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: howarth.at.gcc at gmail dot com While porting rstudio 0.98.1103 to build under clang/libc++, I uncovered a defect in the libstdc++ handling of input iterators by std::search (https://llvm.org/bugs/show_bug.cgi?id=23307). The reduced test case is silently generating wrong code when its std::search is given an input iterator (it assumes it can save the iterator position and backtrack). #include <string> #include <iterator> #include <algorithm> void f(std::istream &s, std::string x) { std::istreambuf_iterator<char> eod; std::search(std::istreambuf_iterator<char>(s), eod, x.begin(), x.end()); } Clang with libc++ properly rejects this with the error... In file included from test3.cpp:1: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:604: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:849:22: error: implicit instantiation of undefined template 'std::__1::basic_istream<char, std::__1::char_traits<char> >' : __sbuf_(__s.rdbuf()) {} ^ test3.cpp:6:19: note: in instantiation of member function 'std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >::istreambuf_iterator' requested here std::search(std::istreambuf_iterator<char>(s), ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:109:33: note: template is declared here class _LIBCPP_TYPE_VIS_ONLY basic_istream; ^ In file included from test3.cpp:1: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1572:12: error: no matching function for call to '__search' return _VSTD::__search<typename add_lvalue_reference<_BinaryPredicate>::type> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:358:15: note: expanded from macro '_VSTD' #define _VSTD std::_LIBCPP_NAMESPACE ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1586:19: note: in instantiation of function template specialization 'std::__1::search<std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::__wrap_iter<char *>, std::__1::__equal_to<char, char> >' requested here return _VSTD::search(__first1, __last1, __first2, __last2, __equal_to<__v1, __v2>()); ^ test3.cpp:6:12: note: in instantiation of function template specialization 'std::__1::search<std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, std::__1::__wrap_iter<char *> >' requested here std::search(std::istreambuf_iterator<char>(s), ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1456:1: note: candidate function [with _BinaryPredicate = std::__1::__equal_to<char, char> &, _RandomAccessIterator1 = std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, _RandomAccessIterator2 = std::__1::__wrap_iter<char *>] not viable: no known conversion from 'typename std::iterator_traits<istreambuf_iterator<char, char_traits<char> > >::iterator_category' (aka 'std::__1::input_iterator_tag') to 'std::__1::random_access_iterator_tag' for 6th argument __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1419:1: note: candidate function [with _BinaryPredicate = std::__1::__equal_to<char, char> &, _ForwardIterator1 = std::__1::istreambuf_iterator<char, std::__1::char_traits<char> >, _ForwardIterator2 = std::__1::__wrap_iter<char *>] not viable: no known conversion from 'typename std::iterator_traits<istreambuf_iterator<char, char_traits<char> > >::iterator_category' (aka 'std::__1::input_iterator_tag') to 'std::__1::forward_iterator_tag' for 6th argument __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, ^ 2 errors generated.