C++标准保证' rdbuf的返回值'传递给流量输出操作员,他的缓冲区孔被打印出来

发布于 2025-01-30 08:31:21 字数 388 浏览 3 评论 0原文

考虑以下代码段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2025-02-06 08:31:21

std :: basic_stringbuf是从a 。 CPPReference描述了其用途:

i/o流对象std :: basic_istreamstd :: basic_ostream以及从它们得出的所有对象(std :: ofStream < /code>,std :: stringstream等)完全根据std :: basic_streambuf

实现

这意味着什么?好吧,让我们看一下std :: basic_istream ::操作员&lt;&lt; 在这里

basic_ostream&amp;操作员&lt;&lt;(std :: basic_streambuf&lt;图表,特征&gt;* sb);
(10)

表现为unformattedutputfunction。构造和检查哨兵对象后,检查sb是否是无效指针。如果是这样,请执行setState(badbit)和退出。否则,从sb控制的输入序列中提取字符,并将其插入*此直到满足以下条件之一:

  • 文件结束发生在输入序列上;
  • 插入输出序列失败(在这种情况下,要插入的字符不会提取);
  • 发生例外(在这种情况下,捕获了例外)。

如果未插入字符,则执行setState(failbit)。如果在提取时抛出异常,请在exceptions()中设置failbit ,如果failbit,请重新调整异常。

因此,是的,这是由std :: cout&lt;&lt;的标准保证的。 ss.rdbuf();将具有您观察到的效果。

A std::basic_stringbuf is derived from a std::basic_streambuf. Cppreference describes its use:

The I/O stream objects std::basic_istream and std::basic_ostream, as well as all objects derived from them (std::ofstream, std::stringstream, etc), are implemented entirely in terms of std::basic_streambuf.

What does that mean? Well, let's take a look at the overload set for std::basic_istream::operator<< here:

basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb );
(10)

Behaves as an UnformattedOutputFunction. After constructing and checking the sentry object, checks if sb is a null pointer. If it is, executes setstate(badbit) and exits. Otherwise, extracts characters from the input sequence controlled by sb and inserts them into *this until one of the following conditions are met:

  • end-of-file occurs on the input sequence;
  • inserting in the output sequence fails (in which case the character to be inserted is not extracted);
  • an exception occurs (in which case the exception is caught).

If no characters were inserted, executes setstate(failbit). If an exception was thrown while extracting, sets failbit and, if failbit is set in exceptions(), rethrows the exception.

So, yes, it's guaranteed by the standard that std::cout << ss.rdbuf(); will have the effect you observed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文