Tuesday, 13 August 2013

Why does inserting a std::basic_streambuf* to an ostream insert the buffer content?

Why does inserting a std::basic_streambuf* to an ostream insert the buffer
content?

Take this simple example program:
// main.cpp
#include <iostream>
#include <fstream>
int main(int argc, const char *argv[])
{
using namespace std;
fstream infile("main.cpp");
basic_streambuf<char> *buf = infile.rdbuf();
cout << static_cast<void *> (buf) << endl;
cout << buf;
}
In order to print the actual address of the basic_streambuf<> object I had
to explicitly cast it to a void *. So the main question is, why does C++
treat basic_streambuf<> like it's some kind of const char *? Is there some
kind of implicit conversion happening or what kind of black voodoo is
this?
Checking the usual online references like cplusplus and en.cppreference
does not show that std::basic_streambuf provides any public conversion
operators. Is there something I'm overlooking?

No comments:

Post a Comment