C++ 转换boost::iostream::stream<;boost::iostreams::source>;到std::i流

C++ 转换boost::iostream::stream<;boost::iostreams::source>;到std::i流,c++,boost,iostream,boost-iostreams,C++,Boost,Iostream,Boost Iostreams,我想在代码中公开流作为它们的标准等价物,以消除用户对boost::iostreams的依赖。 当然,如果必要的话,我们希望在不创建副本的情况下高效地执行此操作。我曾考虑将std::istream的缓冲区设置为boost::iostream::stream正在使用的缓冲区,但是,这可能会导致所有权问题。 如何将boost::iostream转换为std::iostream等效格式? 特别是boost::iostream::stream到std::istream无需转换: #include <

我想在代码中公开流作为它们的标准等价物,以消除用户对
boost::iostreams
的依赖。 当然,如果必要的话,我们希望在不创建副本的情况下高效地执行此操作。我曾考虑将
std::istream
的缓冲区设置为
boost::iostream::stream
正在使用的缓冲区,但是,这可能会导致所有权问题。 如何将
boost::iostream
转换为
std::iostream
等效格式?
特别是
boost::iostream::stream
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 world\nbye world";
    io::array_source source(buf, strlen(buf));
    io::stream<io::array_source> is(source);

    foo(is);
}
std::istream wrap(is.rdbuf());
foo(wrap);

打印相同的

无需转换:

#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 world\nbye world";
    io::array_source source(buf, strlen(buf));
    io::stream<io::array_source> is(source);

    foo(is);
}
std::istream wrap(is.rdbuf());
foo(wrap);
打印相同的