C++分配器,特别是将构造函数参数传递给使用 boost::interprocess::cached_adaptive_pool 分配的对象
这是一个令人尴尬的问题,但即使是 boost.interprocess 提供的写得很好的文档也不足以让我弄清楚如何做到这一点。
我拥有的是 cached_adaptive_pool分配器实例,我想用它来构造一个对象,并传递构造函数参数:
struct Test {
Test(float argument, bool flag);
Test();
};
// Normal construction
Test obj(10, true);
// Normal dynamic allocation
Test* obj2 = new Test(20, false);
typedef managed_unique_ptr<
Test, boost::interprocess::managed_shared_memory>::type unique_ptr;
// Dynamic allocation where allocator_instance == cached_adaptive_pool,
// using the default constructor
unique_ptr obj3 = allocator_instance.allocate_one()
// As above, but with the non-default constructor
unique_ptr obj4 = allocator_instance ... ???
这很可能是我在如何使用分配器对象方面的失败。但无论如何,我看不到如何使用这个特定的分配器,以及 cached_adaptive_pool 将构造函数参数传递给我的对象。
cached_adaptive_pool
具有方法: void Construction(const point & ptr, const_reference v)
但我不明白这意味着什么,也找不到使用它的示例。
我的头脑一整天都在模板中游泳,所以即使答案很明显,只要伸出援助之手,我将不胜感激。
This is an embarrassing question, but even the well-written documentation provided with boost.interprocess hasn't been enough for me to figure out how to do this.
What I have is a cached_adaptive_pool allocator instance, and I want to use it to construct an object, passing along constructor parameters:
struct Test {
Test(float argument, bool flag);
Test();
};
// Normal construction
Test obj(10, true);
// Normal dynamic allocation
Test* obj2 = new Test(20, false);
typedef managed_unique_ptr<
Test, boost::interprocess::managed_shared_memory>::type unique_ptr;
// Dynamic allocation where allocator_instance == cached_adaptive_pool,
// using the default constructor
unique_ptr obj3 = allocator_instance.allocate_one()
// As above, but with the non-default constructor
unique_ptr obj4 = allocator_instance ... ???
This may very well be a failure on my part on how to use allocator objects in general. But in any case, I cannot see how to use this specific allocator, with the interface specified in cached_adaptive_pool to pass constructor arguments to my object.
cached_adaptive_pool
has the method: void construct(const pointer & ptr, const_reference v)
but I don't understand what that means and I can't find examples using it.
My head has been swimming in templates all day, so a helping hand, even if the answer is obvious, will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它应该遵循
std::allocator
的接口,在这种情况下,allocate()
会为您提供一块合适的未初始化内存,并且construct()
在给定的指针上调用placement new。比如:
不过我自己没用过那些泳池。在 C++0x 中,分配器应该能够调用任何构造函数,而不仅仅是复制构造函数,因此 boost 的分配器可能已经在一定程度上支持这一点。
It should follow the interface of
std::allocator
, in which caseallocate()
gives you a suitable chunk of uninitialized memory andconstruct()
calls placement new on the given pointer.Something like:
Haven't used those pools myself, though. In C++0x, allocators should be able to call any constructor, not just the copy constructor, so it might be that boost's allocators already support this to an extent.
我想我总是可以使用 placement new 语法。这个想法是取消引用分配器返回的智能指针(在本例中为 offset_ptr),然后将原始地址传递给 new()。
这是这样做的惯用方式吗? boost中有很多其他地方提供了明确的支持以避免使用placement new,这让我认为不是。无论如何,如果在不久的将来没有提供更好的信息,我会接受这个答案。
I guess I can always use placement new syntax. The idea is to dereference the smart pointer (in this case offset_ptr) returned by the allocator, and then pass the raw address to new().
Is this the idiomatic way of doing this? There are so many other places in boost where explicit support is provided to avoid using placement new, which makes me think not. In any case, I'll accept this answer if nothing better is provided in the near future.