是否可以将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&的函数。

最佳答案

目前尚不清楚您要达到的目标¹,这是我的最佳猜测:

² 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不支持共享内存

10-06 01:42