angry ferret
angry ferret

Reputation: 57

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?

Upvotes: 1

Views: 647

Answers (1)

catscradle
catscradle

Reputation: 1719

http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

ostream& operator<< (streambuf* sb );

Retrieves as many characters as possible from the input sequence controlled by the stream buffer object pointed by sb (if any) and inserts them into the stream, until either the input sequence is exhausted or the function fails to insert into the stream.

http://www.cplusplus.com/reference/streambuf/streambuf/

It is an instantiation of basic_streambuf with the following template parameters: charT = char

Upvotes: 2

Related Questions