istringstream.rdbuf with custom streambuf does not compile
杨岑
ssawgift@163.com
Fri May 6 15:26:00 GMT 2016
Thanks for the iss.istream::rdbuf tip. I never knew C++ syntax has
evolved so much.
å¨ 2016/5/6 23:12, Martin Sebor åé:
> 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