我正在使用 boost::interprocess::offset_ptr<> 模板类特化,作为我结构中多个字段的一种类型。不幸的是 offset_ptr 有一个底层指针的大小(在我的例子中是 8 个字节)。但是我确信,它永远不会超过 4 字节整数的最大大小。
所以我的问题来了。我能否以某种方式轻松创建 4 字节范围的 offset_ptr,或者我将不得不转换为 4 字节宽度的整数或从中转换?
最佳答案
offset_ptr
默认模板参数是
static const std::size_t offset_type_alignment = 0;
template <class T, class DifferenceType = std::ptrdiff_t,
class OffsetType = std::size_t, std::size_t Alignment = offset_type_alignment>
class offset_ptr;
您可以更改 offset_ptr
的第三个参数。
#include <iostream>
#include <boost/interprocess/offset_ptr.hpp>
#include <cstdint>
int main()
{
using namespace boost::interprocess;
offset_ptr<int, std::ptrdiff_t, std::uint32_t> offs;
std::cout << sizeof(offs) << std::endl;
}
关于c++ - 如何缩小 boost::offset_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019653/