2012-08-09 100 views
4

在C++ 11中auto_ptr已棄用,由合理的unique_ptr替代。唉,如果你使用的boost::ptr_map滿足auto_ptr一個非常方便的使用:是否有替代auto_ptr的,可以在C++中使用boost ptr_map 11

std::auto_ptr<Layer> pLayer(new Layer()); 
mRawLayerPtrMap.insert(layerName,pLayer); 

是否有使用與C++ 11類似的東西的可能性。 我知道

Layer* pLayer = new Layer(); 
mFusedLayers.insert(fusedLayerName,pLayer); 

作品,但auto_ptr了它在一些更復雜的場景優點。 是否有可以與C++ 11兼容的替代品?

+1

究竟阻止您使用的unique_ptr? – bames53 2012-08-09 21:44:04

+0

增強沒有插入功能,至少不是在1.46中,只要沒有記錄在新版本中,但我沒有檢查源代碼 – Martin 2012-08-09 21:46:49

+0

查看關於ptr_map動機的boost文檔看起來沒有理由使用它來代替unique_ptr的常規地圖。 – bames53 2012-08-09 21:51:34

回答

8

如何

std::unique_ptr<Layer> pLayer(new Layer()); 
mFusedLayers.insert(fusedLayerName,pLayer.release()); 
+0

很好地謝謝。即使mFusedLayers.insert(fusedLayerName,std :: unique_ptr (new Layer())。release());順利下去 – Martin 2012-08-09 21:55:29

+0

爲什麼不移動()? – 2012-08-09 21:57:25

+0

@LokiAstari確實如此。在這種情況下幕後會發生什麼事情? – Martin 2012-08-09 22:00:14

相關問題