gpt4 book ai didi

c++ - C++ streambuf 方法可以抛出异常吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:29:54 30 4
gpt4 key购买 nike

我正在尝试找到一种方法来获取读取或写入流的字符数,即使出现错误并且读/写结束很短也是可靠的。

我正在做这样的事情:

return stream.rdbuf()->sputn(buffer, buffer_size);

但是如果允许 overflow 的 streambuf 实现抛出异常,这将不起作用。是吗?我无法在任何地方找到它的记录。

最佳答案

basic_streambuf::overflow 允许在失败时抛出异常,如 27.6.3.4.5/6 中所述,遗憾的是没有办法确保 编译时该函数永远不会抛出异常。

看来您的运气已经不多了,要 100% 确定 overflow 不会抛出异常的唯一方法是编写您自己的 streambuf失败时不会这样做。


[27.6.3.4.5/2-3] int_type overflow(int_type = c = traits::eof ())
...

[27.6.3.4.5/5]

Requires: Every overriding definition of this virtual function shall obey the following constraints:

1) The effect of consuming a character on the associated output sequence is specified309

2) Let r be the number of characters in the pending sequence not consumed. If r is non-zero then pbase() and pptr() shall be set so that: pptr() - pbase() == r and the r characters starting at pbase() are the associated output stream. In case r is zero (all characters of the pending sequence have been consumed) then either pbase() is set to NULL, or pbase() and pptr() are both set to the same NULL non-value.

3) The function may fail if either appending some character to the associated output stream fails or if it is unable to establish pbase() and pptr() according to the above rules.

[27.6.3.4.5/6]

Returns: traits::eof () or throws an exception if the function fails

关于c++ - C++ streambuf 方法可以抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19105657/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com