使用整数标识符在共享内存上构造对象

发布于 2024-09-30 02:22:51 字数 258 浏览 1 评论 0原文

当我查看 boost interprocess 库的示例时,对象始终是用字符串名称构造的。

segment.construct<Equipments>("name")("param1", "param2");

是否可以使用整数标识符构造对象,例如

segment.construct<Equipments>(123)("param1", "param2");

When I look at the samples of boost interprocess library, the objects are always constructed with string name.

segment.construct<Equipments>("name")("param1", "param2");

Is it possible to construct the object with integer identifier like

segment.construct<Equipments>(123)("param1", "param2");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-10-07 02:22:51

你为什么要这样做?无论如何,你可以用一种“有点”肮脏的方式来做到这一点:

segment.construct<Equipments>((char*)123)("param1", "param2");

只要 sizeof(char*) >= sizeof(int) 在你的平台上(在 Win32/Win64 上),就可以安全地转换指针返回整数而不丢失信息。请确保您没有将指针值用作有效的 char* 指针。

哦,您必须确保底层库代码不会尝试将该值用作有效的字符指针。

Why would you want to do this? In any case, you can do it in a "bit" dirty way:

segment.construct<Equipments>((char*)123)("param1", "param2");

As long as sizeof(char*) >= sizeof(int) on your platform (which it is on Win32/Win64), the pointer can be safely cast back to the integer without loss of information. Just be sure that you don't use the pointer value as a valid char* pointer.

Oh, and you have to be sure that the underlying library code will not try to use the value as a valid char pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文