2012-04-07 65 views
2

我嘗試做以下事情:升壓共享存儲型

創建一個「大」陣列(1個000 000 +對象)與升壓::進程間圖書館

我的代碼包含了共享內存以下:

managed_shared_memory testarray(create_only, "Test", 45000000); 

typedef std::pair<SingleField, uint32_t> TestType; 
TestType * test = testarray.construct<TestType>("TestArray")[45000000]; 

我的問題是:我如何找出這個升壓函數的返回類型是什麼?

如果我在上面用以下方法做同樣的事情:SingleField而不是「::對它似乎不工作,但我不需要第二個容器,我只需要一個,但一個它不工作!

eclipse的輸出對我來說太神祕了,由於我使用boost,因爲這些問題我已經停止了好幾次了,有沒有一種簡單的方法來找出函數將返回的「Type」? (我來自Java的所以我已經習慣了有一些‘簡單’的定義,說對象X)其實,我會很高興,如果我能找出哪些特定的函數返回時,與我寫我自己的所有功能,這是類型簡單但有了這個庫我似乎有問題。

第二個問題:爲什麼這些例子總是以「型」對,它是一個圖書館預條件?

- >我一直在使用的#include嘗試,Eclipse的告訴我它的std ::對的問題是,爲什麼是T *?這是起始段地址嗎?

感謝您的時間&答案。

Eclipse的輸出:

Multiple markers at this line 
- unused variable test 
- cannot convert const 
boost::interprocess::detail::named_proxy<boost::interprocess::segment_manager<char, 
boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, 
boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, Field, false> to 
SharedMemoryInitializer::Create()::TestType* in initialization 

我看過Boost庫手冊幾次,也許我期待在錯誤的網站或網頁,如果您提供的我想念的信息我會非常高興。

http://www.boost.org/doc/libs/1_42_0/doc/html/interprocess/quick_guide.html#interprocess.quick_guide.qg_interprocess_container

+0

我沒有使用升壓。之前的Interprocess,但如果你需要在C++中獲得函數的返回類型,你總是可以使用'decltype'或'std :: result_of'。在這裏看到更多的信息:http://stackoverflow.com/questions/2689709/difference-between-stdresult-of-and-decltype。 – 2012-04-07 20:17:38

+0

所以這基本上是在新的c + +標準中使用的,我曾嘗試使用關鍵字「自動」,我猜我的版本不符合新的C++標準,我會給它一個decltype – 2012-04-07 20:46:24

+1

你可能不得不指定編譯器使用最新標準的附加編譯標誌。對於g ++ - 4.6,你可以在g ++ - 4.7中使用'-std = C++ 0x',以及'-std = C++ 11'。 – 2012-04-08 01:15:33

回答

0

從我的角度來看,也有你的代碼有兩個主要問題:

  • 你似乎分配TestType類型的4500個對象這可能不是你想要的(除非TestType只需要每個實例一個字節):

    TestType * test = testarray.construct<TestType>("TestArray")[45000000];

  • 根據文檔(*),則必須提供爲構造函數調用括號(您使用的是第二版): TestType * test = testarray.construct<TestType>("TestArray")[45000000]();

    我認爲TestType有一個無參數的構造函數。

(*)http://www.boost.org/doc/libs/1_46_1/doc/html/interprocess/managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.allocation_types

+0

感謝您的回答,主要錯誤是45 000 000,我認爲它是字節,但它實際上是「對象」 – 2012-05-15 05:09:16