2010-10-06 54 views
5

如果我想創建一個unique_ptr類型QueueList(某些自定義對象),我該如何爲它定義一個deletor或者是否已經有一個我可以使用的模板'Deletor'?boost unique_ptr Deletor

我想要一個unique_ptr所以我可以安全地在線程之間傳輸對象,而不必在線程之間共享它。

編輯

boost::interprocess::unique_ptr<QueueList> LIST; ///FAILS to COMPILE!!! 

LIST mylist; 

編譯:MS Visual Studio 2003中

錯誤:

錯誤C2976: '升壓::進程間::的unique_ptr':太少模板參數

錯誤C2955:'boost :: interprocess :: unique_ptr':使用類模板需要模板參數列表 :參見'boost :: interprocess聲明::的unique_ptr」

+0

你如何分配QueueList對象?它應該如何刪除?考慮提供示例代碼。 – sellibitze 2010-10-06 14:55:07

+1

我們能否擁有編譯器名稱/版本以及確切的錯誤?除此之外,我沒有看到任何錯誤,除了使用非宏的所有大寫字母。 – 2010-10-06 15:10:45

回答

9

下面是一個簡單的刪除器類,它只是調用刪除任何給定的對象:

template<typename T> struct Deleter { 
    void operator()(T *p) 
    { 
     delete p; 
    } 
}; 

可以再用的unique_ptr這樣使用它:

boost::interprocess::unique_ptr<QueueList, Deleter<QueueList> > LIST; 
+0

這不是默認的刪除器嗎? – Amnon 2010-11-22 10:48:53

+1

嗯......看起來像增強沒有默認的刪除。 – Amnon 2010-11-22 10:55:55