C ++分配器,特别是通过构造函数参数与升压::进程间分配的对象:: cached_adaptive_pool [英] C++ allocators, specifically passing constructor arguments to objects allocated with boost::interprocess::cached_adaptive_pool

查看:112
本文介绍了C ++分配器,特别是通过构造函数参数与升压::进程间分配的对象:: cached_adaptive_pool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个令人尴尬的问题,但即使提供Boost.Interprocess中精心编写的文档,还不足以让我弄清楚如何做到这一点。

我已经是一个 cached_adaptive_pool 分配器实例,我想用它来构造一个对象,沿构造函数的参数传递:

 结构测试{
  测试(float参数,布尔标志);
  测试();
};//正常施工
测试OBJ(10,TRUE);
//正常动态分配
测试* OBJ2 =新的测试(20,FALSE);typedef的managed_unique_ptr<
    测试的boost ::进程间:: managed_shared_memory> ::类型的unique_ptr;//动态分配allocator_instance == cached_adaptive_pool,
使用默认构造函数//
的unique_ptr obj3 = allocator_instance.allocate_one()
//同上,但与非默认的构造
的unique_ptr OBJ4 = allocator_instance ...?

这很可能是我对如何使用一般分配器对象的一部分失败。但在任何情况下,我不能看到如何使用这个特定的分配器,在的 cached_adaptive_pool 来构造函数参数传递给我的对象。

cached_adaptive_pool 的方法:无效构造(常量指针和放大器,PTR,为const_reference V),但我不不懂是什么意思,并使用它,我无法找到的例子。

我的头已经在模板整天在游泳,于是伸出援助之手,即使答案是显而易见的,会大大AP preciated。


解决方案

  

cached_adaptive_pool有方法:
  无效结构(常量指针和放大器; PTR,
  为const_reference V),但我不知道
  明白这意味着什么,我不能
  使用它找到的例子。


应该遵循)<$​​ C $ C>的std ::分配器的接口,在这种情况下,分配(为您提供了一个合适的未初始化的内存和构造的块()调用新的位置给定的指针上。

是这样的:

  allocator_instance.construct(allocator_instance.allocate_one(),测试(30,真正的));

没有使用这些池自己,虽然。在C ++ 0x中,分配器应该能够调用任何构造,不只是拷贝构造函数,所以它可能是升压转换器的分配器已经支持这在一定程度上。

  a.construct(P,30,真正的); //一个C ++ 0x的分配将允许这一点,调用新的(P)测试(30,真)

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.

解决方案

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)

这篇关于C ++分配器,特别是通过构造函数参数与升压::进程间分配的对象:: cached_adaptive_pool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆