c++ - 如何缩小 boost::offset_ptr

标签 c++ boost offset boost-interprocess

我正在使用 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/

相关文章:

c++ - 有没有可能不继承自 boost::operators,但仍然使用它?

javascript - 谷歌地图 API : Marker image positioning

javascript - jQuery 获取相对于特定 div 的顶部偏移量

c++ - Console::WriteLine() 与 cout

c++ - 检查 std::vector 中的字符串是否包含 C++ 中的子字符串

c++ - <cstdint> 与 <stdint.h>

python - 为什么 offsets.QuarterBegin 日期是 12-01、03-01、06-01、09-01?

c++ - 对于 "busybox"样式的应用程序,这种全局静态初始化的使用是否合适?

c++ - 用 boost::spirit 解析成一个 vector<vector<double>>

具有包装随机访问的 C++ vector ?