c++ - 移动而不是复制 boost::dynamic_bitset BlockInputIterator 构造函数?

标签 c++ boost

是否可以创建另一个 boost::dynamic_bitset BlockInputIterator 构造函数以使用移动而不是复制 std::vector

我相信以下构造函数使用复制 boost docs :

template <typename BlockInputIterator>
dynamic_bitset(
    BlockInputIterator first,
    BlockInputIterator last,
    const Allocator& alloc = Allocator());

这是我写的一些代码来显示我的要求:

#include <iostream>
#include <vector>
#include <boost/dynamic_bitset.hpp>

using namespace std;
using namespace boost;

int main(int argc, char* argv[])
{
    // Notice the vector opposed to dynamic_bitset
    vector<uint8_t> data;

    // put in dummy data
    data.push_back(0x1a);
    data.push_back(0xcf);
    data.push_back(0xfc);
    data.push_back(0x1d);
    for (auto i = data.begin(); i != data.end(); ++i)
    {
      cout << hex << (int)*i << dec << ' ';
    }
    cout << std;
    cout << "data.data(): " << hex << (uint64_t)data.data() << dec << endl;

    // I believe this is a copy opposed to a move
    boost::dynamic_bitset<uint8_t> bs0(data.begin(), data.end());

    // I would like to be able to do this which would use move,
    // is that possible considering the vector?
    boost::dynamic_bitset<uint8_t> bs1 = data;

    return 0;
}

所以基本上我想知道 boost 是否可以向 boost::dynamic_bitset 添加一个额外的构造函数以使用移动而不是复制 std::vector

最佳答案

除非 vector 的界面发生变化。

你看,运动对于一个物体来说是一个非常亲密的操作;这就是为什么移动支持需要该类型的成员函数。这不是您可以从外部强加到物体上的东西。

vector 无法采用任意的、用户分配的内存缓冲区作为其内部存储。理论上可以通过某种方式要求它这样做,但目前没有这样的接口(interface)。没有这样的支持,其他代码无法为 std::vector 提供内存。

Boost 或许能够提供boost::container::vector 这样的接口(interface)。但这不会改变 std::vector

同样,您不能带着 std::vector 的内存存储潜逃。好吧,你可以,但只能通过分配器欺骗。也就是说,当 vector 要求分配器实际解除分配或销毁对象时,您必须告诉分配器不要实际解除分配或销毁对象。这将很棘手,因为 vector::get_allocator 返回分配器的拷贝。

当然,dynamic_bitset 也没有办法获得分配;它希望拥有它。很像 vector。因此,即使您可以利用 std::vector 的存储潜逃,dynamic_bitset 也无法采用它。

关于c++ - 移动而不是复制 boost::dynamic_bitset BlockInputIterator 构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34778000/

相关文章:

C++ 继承、接口(interface)

c++ - CopyResource 从一个 D3D11 设备到另一个

c++ - Boost 链接错误 undefined reference to GLIBCXX_3.4

c++ - Boost.Spirit HTTP header 解析器、跟踪

java - 如何诊断 Windows Vista 中的 Java JNI EXCEPTION_ACCESS_VIOLATION 错误

c++ - 生成器列表与 C++ 标准库?

c++ - 逗号运算符的正确用法是什么?

c++ - 使用 boost::asio::streambuf 缺少第一个字符

c++ - 列出我的计算机上安装的物理驱动器

c++ - 无法使用 boost::asio 收听 UDP 端口