为什么在ostream中插入std :: basic_streambuf *会插入缓冲区内容?

时间:2013-08-14 00:53:51

标签: c++ iostream

采用这个简单的示例程序:

// 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;
}

为了打印basic_streambuf<>对象的实际地址,我必须将其明确地转换为void *。所以主要的问题是,为什么C ++会将basic_streambuf<>视为某种const char *?是否有某种隐式转换发生或者这是什么样的黑巫术?

检查cplusplusen.cppreference等常用在线参考文件并未显示std::basic_streambuf提供任何公共转换运算符。我有什么东西可以忽略吗?

1 个答案:

答案 0 :(得分:2)

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