C ++ streambuf方法可以抛出异常吗? [英] Can C++ streambuf methods throw exceptions?

查看:162
本文介绍了C ++ streambuf方法可以抛出异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to find a method to get the number of characters read or written to a stream that is reliable even if there is an error and the read/write ends short.

我这样做:

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

但是如果 overflow 的streambuf实现是允许抛出豁免这不会工作。是吗?我无法在任何地方找到它。

but if the streambuf implementation of overflow is permitted to throw excpections this won't work. Is it? I've not been able to find it documented anywhere.

推荐答案

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

basic_streambuf::overflow is allowed to throw an exception upon failure as documented in 27.6.3.4.5/6, and sadly there's no way to ensure compile-time that the function won't ever throw an exception.

似乎你运气不好,唯一的办法是100%确保溢出不会抛出异常是写自己的 streambuf ,只是不这样做失败。

Seems like you are running out of luck and the only way to be 100% sure that overflow won't throw an exception is to write your own streambuf that just doesn't do that upon failure.


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

...

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


[ 27.6.3.4.5 / em>]

[27.6.3.4.5/5]

需要:此虚拟函数的每个重写定义都必须遵守以下约束:

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

1)指定在相关输出
序列上使用字符的效果309

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

2)令r为未使用序列
。如果r是非零,那么pbase()和pptr()应设置为
:pptr() - pbase()
== r和从pbase输出流。如果r是零(待处理的
序列的所有字符都已被消费),那么pbase()被设置为NULL,或者
pbase()和pptr()被设置为相同的NULL -value。

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)如果向
关联的输出流附加一个字符失败或者无法建立pbase(),函数可能失败b $ b和pp​​tr()根据上述规则。

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 ]

退货 traits :: eof()如果函数失败则抛出异常

这篇关于C ++ streambuf方法可以抛出异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆