将 boost::iostream::stream<boost::iostreams::source> 转换为 std::istream
convert boost::iostream::stream<boost::iostreams::source> to std::istream
我想在我的代码中公开流作为它们的标准等价物,以消除用户对boost::iostreams
的依赖。当然,希望有效地执行此操作,而无需在必要时创建副本。我想过将std::istream
的缓冲区设置为boost::iostream::stream<boost::iostreams::source>
使用的缓冲区,但是,这可能会导致所有权问题。如何将boost::iostream
转换为等效std::iostream
?特别boost::iostream::stream<boost::iostreams::source>
std::istream
.
无需转换:
住在科里鲁
#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>
namespace io = boost::iostreams;
void foo(std::istream& is) {
std::string line;
while (getline(is, line)) {
std::cout << " * '" << line << "'n";
}
}
int main() {
char buf[] = "hello worldnbye world";
io::array_source source(buf, strlen(buf));
io::stream<io::array_source> is(source);
foo(is);
}
除此之外,我认为您不会遇到所有权问题,因为std::istream
在分配新的 rdbuf 时不会承担所有权:
- 为什么 std::istream 不对其 streambuf 拥有所有权?
因此,您也可以自由地执行以下操作:
住在科里鲁
std::istream wrap(is.rdbuf());
foo(wrap);
打印相同
相关文章:
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Boost buffer_cast 无法从 void* 转换为 PointerToPodType
- Boost.Spirit将表达转换为AST
- 将 boost 序列化对象的 asio::streambuf 表示转换为 Beast 的 DynamicBody req.body()
- 将"boost::tuple "转换为"boost::fusion::tuple&quo
- 用于 boost::p roperty_tree 的通用枚举转换器
- 如何将 boost::hana::map 转换为 lambda
- 如何将 boost::hana::tuple 转换为 std::变体
- 将 boost::文件系统::p ath 转换为 char?
- Boost.Hana:在 constexpr 上下文中将值元组转换为相应类型的元组
- 为什么要 boost::p roperty_tree::write_json() 将整数值转换为字符串?这是不正确的
- 如何正确地将boost::asio::buffers_iterator转换为InputIterator
- 如何将boost::asio::ip::address_v6 ip转换为2个uint64_t数字,并从2个uint 64
- 如何将 boost::beast 的响应<buffer_body>转换为响应<string_body>?
- 为boost::dynamic_bitset生成哈希,并将哈希转换回boost::dynamic_bitset
- 如何将 time-uuid(存储在 boost uuid 中)转换为时间戳/自纪元以来的时间?
- 将 boost:::chrono::steady_clock::time_point 转换为标准::时间::steady
- n-ary布尔语法从中缀到前缀的Boost::Spirit转换?
- 寻找Boost转换迭代器的复合特征模式
- 使用boost转换度、分、秒、弧度