這是一個令人尷尬的問題,但即使是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