C++分配器,特别是将构造函数参数传递给使用 boost::interprocess::cached_adaptive_pool 分配的对象

发布于 2024-08-28 05:27:10 字数 1246 浏览 6 评论 0原文

这是一个令人尴尬的问题,但即使是 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 技术交流群。

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

发布评论

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

评论(2

捶死心动 2024-09-04 05:27:10

cached_adaptive_pool 有以下方法:
void 构造(常量指针和 ptr,
const_reference v) 但我不
明白这意味着什么,但我不明白
查找使用它的示例。

它应该遵循 std::allocator 的接口,在这种情况下,allocate() 会为您提供一块合适的未初始化内存,并且 construct()在给定的指针上调用placement new。

比如:

allocator_instance.construct(allocator_instance.allocate_one(), Test(30, true));

不过我自己没用过那些泳池。在 C++0x 中,分配器应该能够调用任何构造函数,而不仅仅是复制构造函数,因此 boost 的分配器可能已经在一定程度上支持这一点。

a.construct(p, 30, true); //a C++0x allocator would allow this and call new (p) Test(30, true)

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.

It should follow the interface of std::allocator, in which case allocate() gives you a suitable chunk of uninitialized memory and construct() calls placement new on the given pointer.

Something like:

allocator_instance.construct(allocator_instance.allocate_one(), Test(30, true));

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.

a.construct(p, 30, true); //a C++0x allocator would allow this and call new (p) Test(30, true)
夜吻♂芭芘 2024-09-04 05:27:10

我想我总是可以使用 placement new 语法。这个想法是取消引用分配器返回的智能指针(在本例中为 offset_ptr),然后将原始地址传递给 new()。

unique_ptr obj = new(&(*allocator_instance.allocate_one())) Test(1,true)

这是这样做的惯用方式吗? 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().

unique_ptr obj = new(&(*allocator_instance.allocate_one())) Test(1,true)

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.

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