C++标准保证' rdbuf的返回值'传递给流量输出操作员,他的缓冲区孔被打印出来
考虑以下代码段:
std::stringstream ss;
ss << "hello world!\n";
auto a = ss.rdbuf();
std::cout << a; // prints out "hello world!
变量a
是指向类型std :: StringBuf
的对象的指针。当将其传递给流量输出操作员&lt;&lt;
,使用GCC9.4时,a
指向的流缓冲区的内容被打印出来。
我的问题是:这种行为是从GCC中实现的std :: StringBuf
的事故,还是语言标准保证这将始终起作用?
Consider the following code snippet:
std::stringstream ss;
ss << "hello world!\n";
auto a = ss.rdbuf();
std::cout << a; // prints out "hello world!
The variable a
is a pointer to an object of the type std::stringbuf
. When it is passed to the stream output operator <<
, with GCC9.4, the content of the stream buffer pointed by a
gets printed out.
My question is: is this behavior just an accident from the way std::stringbuf
is implemented in GCC, or does the language standard guarantee this will always work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std :: basic_stringbuf
是从a 。 CPPReference描述了其用途:这意味着什么?好吧,让我们看一下
std :: basic_istream ::操作员&lt;&lt;
在这里:因此,是的,这是由
std :: cout&lt;&lt;的标准保证的。 ss.rdbuf();
将具有您观察到的效果。A
std::basic_stringbuf
is derived from a std::basic_streambuf. Cppreference describes its use:What does that mean? Well, let's take a look at the overload set for
std::basic_istream::operator<<
here:So, yes, it's guaranteed by the standard that
std::cout << ss.rdbuf();
will have the effect you observed.