我有一个自定义的 std::streambuf
。像这样的东西:
class mybuf : public std::streambuf {
...
virtual std::streambuf::int_type underflow() {
if(eof) {
return traits_type::eof();
}
...
}
...
std::istream stream(new mybuf(...));
通过在 underflow()
函数上返回 eof 来设置流的 eof
标志。
如何设置流的 badbit
标志? overflow()
函数应该抛出异常吗?
最佳答案
抛出异常确实是可行的方法。
引用页面std::ios_base::iostate
,特别是这个:
The
badbit
is set by the following standard library functions:
- ...
- Every stream I/O function if an exception is thrown by any member function of the associated stream buffer (e.g.
sbumpc()
,xsputn()
,sgetc()
,overflow()
, etc)- ...
关于c++ - 如何通过自定义的 streambuf 设置流的 badbit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50716688/