使用整数标识符在共享内存上构造对象
当我查看 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你为什么要这样做?无论如何,你可以用一种“有点”肮脏的方式来做到这一点:
只要 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:
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.