将 boost 的缓冲流转换为 istream

Convert boost`s bufferstream to istream

是否可以将 boost 的缓冲流转换为 istream?我正在尝试进行转换,但是我仍然不清楚我是做错了什么还是根本不可能这样做。我将不胜感激任何答案。

char *copy = static_cast <char*> (region.get_address());
for (int i = 0;i < length;i++) copy[i] = str[i];
bufferstream input_stream (copy, length);

然后我需要将 bufferstream 转换成 istream。 基本上,我需要将 bufferstream 实例作为参数传递给接受 istream & 的函数。

不清楚你想达到什么¹,这是我最好的猜测:

Live² On Coliru

#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <iostream>

namespace bip = boost::interprocess;

int main() {
    bip::shared_memory_object smo(bip::open_or_create, "MySharedMemory", bip::read_write);

    std::string str = "test data";
    smo.truncate(10ull << 10); // 10 KiB
    bip::mapped_region r(smo, bip::read_write);

    bip::bufferstream stream(reinterpret_cast<char*>(r.get_address()), r.get_size());

    if (stream << str)
        std::cout << "Written";
}

¹ https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem

² Coliru 不支持共享内存