istringstream.rdbuf with custom streambuf does not compile

Martin Sebor msebor@gmail.com
Fri May 6 15:12:00 GMT 2016


On 05/06/2016 06:40 AM, 杨春雷 wrote:
> 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);

To write the code without a cast it's possible to qualify the name
rdbuf with the name of the class where to look up its name:

   iss.istream::rdbuf (&fbuf);

But there an important subtlety to these overloads of rdbuf that's
worth mentioning.  istringstream::rdbuf() always returns a pointer
to the the same stream buffer object (the member of the stream
object).  basic_ios::rdbuf() returns a pointer the basic_ios
subobject was last associated with by calling the one argument
overload of basic_ios::rdbuf(basic_streambuf*) with.  If in the
example above one were to also call rdbuf() to obtain the streambuf
pointer.  To retrieve the basic_ios streambuf pointer, call
istream::rdbuf() (e.g., iss.istream::rdbuf()).

Martin



More information about the Gcc-help mailing list