1

以这个简单的示例程序为例:

// 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提供任何公共转换运算符。有什么我忽略的吗?

4

1 回答 1

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

于 2013-08-14T01:01:15.377 回答