这是一个令人尴尬的问题,但即使是boost.interprocess提供的精心编写的文档也不足以让我弄清楚去做这个。C++的分配器,特别是将构造函数的参数传递给分配给boost :: interprocess :: cached_adaptive_pool的对象::
我已经是一个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 construct(const pointer & ptr, const_reference v)
但我不明白这是什么意思,我找不到使用它的例子。
我的头一整天都在模板中游泳,所以即使答案很明显,我们也会非常感激。
谢谢。第一种形式是我期望的答案,但是太困惑和疲倦,无法意识到自己。第二种形式'a.construct(p,30,true)'似乎没有被这个特定的boost分配器支持。令人讨厌的是,由于'cached_adaptive_pool.construct()'返回'void',我必须写'allocator_t :: pointer obj = allocator.allocate_one(); allocator.construct(obj,Test(30,true))'?我不确定更糟糕的是,使用两个语句进行一个简单的操作,或者直接使用有点奇怪的放置位置。 – porgarmingduod 2010-04-07 22:47:52
你可以做一个单独的函数。 – UncleBens 2010-04-08 07:13:44