3 回答
TA貢獻1799條經驗 獲得超6個贊
共有所有權:shared_ptrweak_ptrweak_ptrshared_ptr
shared_arrayshared_ptr<std::vector<T> const>.
intrusive_ptr
獨特所有權:scoped_ptrstd::unique_ptrboost::scoped_ptrboost::scoped_ptr
scoped_arraystd::unique_ptr<T[]>delete[]deletedefault_deletestd::unique_ptr<T[]>operator[]operator*operator->.
std::auto_ptr§D.10 [depr.auto.ptr]
類模板 auto_ptr不受歡迎。[ 注:類模板 unique_ptr(20.7.1)提供了一個更好的解決方案。 -尾注 ]
無所有權:
shared_ptrweak_ptrshared_ptrlockshared_ptrexpiredexpired
if(!wptr.expired()) something_assuming_the_resource_is_still_alive();
TA貢獻1829條經驗 獲得超7個贊
PersonHouse
std::unique_ptr<T>.
std::shared_ptr<T>.std::shared_ptr<T>std::weak_ptr<T>
T*
T&.
如果您有一個自定義刪除器(例如,您使用分配池),那么這將導致每個指針的開銷,這很容易通過手動刪除來避免。 std::shared_ptr具有復制上的引用計數增量的開銷,加上銷毀時的減少,然后進行0計數檢查并刪除所保存的對象。根據實現的不同,這會使您的代碼膨脹并導致性能問題。 編譯時間。與所有模板一樣,智能指針對編譯時間有負面貢獻。
struct BinaryTree{
Tree* m_parent;
std::unique_ptr<BinaryTree> m_children[2]; // or use std::array...};nullptrstd::unique_ptr.
struct ListNode{
std::shared_ptr<ListNode> m_next;
std::weak_ptr<ListNode> m_prev;};shared_ptrweak_ptr
- 3 回答
- 0 關注
- 537 瀏覽
添加回答
舉報
