istringstream.rdbuf with custom streambuf does not compile

杨春雷 ssawgift@163.com
Fri May 6 12:40:00 GMT 2016


I have the following simple test code (t.cxx):

#include <streambuf>
#include <sstream>
using namespace std;

class FileBuf: public streambuf
{
};

int main(int argc, char** argv)

{
    FileBuf fbuf;
    istringstream iss;
    // The following line does not compile!
    iss.rdbuf(&fbuf);

    return 0;
}

When compiling the above code using g++ (version 5.3.1 20160406 (Red Hat 5.3.1-6)), I got very strange errors:

$ g++ -o t t.cxx
t.cxx: In function ‘int main(int, char**)’:
t.cxx:14:17: error: no matching function for call to ‘std::__cxx11::basic_istringstream<char>::rdbuf(FileBuf*)’
  iss.rdbuf(&fbuf);
                 ^
In file included from t.cxx:2:0:
/usr/include/c++/5.3.1/sstream:472:7: note: candidate: std::__cxx11::basic_istringstream<_CharT, _Traits, _Alloc>::__stringbuf_type* std::__cxx11::basic_istringstream<_CharT, _Traits, _Alloc>::rdbuf() const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_istringstream<_CharT, _Traits, _Alloc>::__stringbuf_type = std::__cxx11::basic_stringbuf<char>]
       rdbuf() const
       ^
/usr/include/c++/5.3.1/sstream:472:7: note:   candidate expects 0 arguments, 1 provided

However, I have to a (unnecessary) cast to get it working, like this:
    ((istream&)iss).rdbuf(&fbuf);

I'm always intimated by C++ template syntax. I wonder what have I done wrong this time.


More information about the Gcc-help mailing list