C++:std::istream read及其对std::streambuf underflow的调用
C++: std::istream read and its calls to std::streambuf underflow
下面的代码只是测试在std::stringbuf
上使用std::istream
read
时调用underflow
的频率。
#include <iostream>
#include <vector>
#include <sstream>
class TestStringBuf :
public std::stringbuf
{
public:
int_type underflow()
{
std::cout<<"TestStringBuf underflow"<<std::endl;
return std::stringbuf::underflow();
}
};
int main(int argc, const char * argv[])
{
TestStringBuf buf;
std::iostream stream(&buf);
stream << "tesr";
std::vector<char> data(4);
stream.read(&data[0], 4);
for(int i=0; i<data.size(); ++i)
std::cout<<data[i];
std::cout<<std::endl;
return 0;
}
输出为:
TestStringBuf underflow
TestStringBuf underflow
test
我希望下溢只被调用一次,因为我准确地读取了 get 区域中存在的字节数,那么为什么它应该再次下溢呢?这是预期的行为吗?我问是因为我的自定义underflow
方法可能会长时间阻止读取新数据,因此在这种情况下,第二次调用underflow
不是很可取。
我在Osx上使用clang 3.1和libc ++。
谢谢!
更新:
我刚刚做了一个完全独立的测试,在我看来,这在libc++实现中是一个奇怪的问题,因为libstd ++不会发生这种情况。 有人可以用其他实现来测试吗? 这是一个错误还是只是一个实现差异(对我来说感觉很错误)。我更新了上面的代码,以便您可以将其复制并粘贴到任何主.cpp中。
更新2:
毕竟这是libc ++中的一个错误,请参阅:http://llvm.org/bugs/show_bug.cgi?id=13113 。如果你自己编译libc ++,这个错误应该会消失,我会尽快尝试。
C++标准明确允许 std::basic_stringbuf 的实现将字符序列存储在 std::basic_string 中,而不是使用 std::basic_streambuf 的内部缓冲区。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 字符 - 'pub' 在 std::streambuf::p ubsetbuf() 方法中是什么意思?
- 将 std::string 转换为 boost::asio::streambuf
- 可以将STD :: StreamBuf的自定义实现中的异常交付给流用户
- 用 std::streambuf 包装 libcurl 以进行上传
- 正确实现 std::streambuf::overflow
- C++:std::istream read及其对std::streambuf underflow的调用
- std::裸机C++中的 streambuf 实例化
- 如何在不复制的情况下将 std::string 的一部分放入 streambuf 中
- 如何实现自定义 std::streambuf 的 seekoff()?
- 将数据从 boost::asio::streambuf 复制到 std::string
- std::istream子类:删除析构函数中的streambuf
- std::streambuf::in_aval对于std::ostream总是返回0
- 将streambuf提升为const char*,使用buffer_cast vs std::string vs ost
- 将数据序列化到std::streambuf
- 如何手动将数据放入boost::asio::streambuf中,以便稍后使用std::istream读取
- asio::streambuf to std::istream 丢弃新行