簡體   English   中英

C ++ 11的shared_ptr分配

[英]C++11's shared_ptr assignment

我有以下代碼:

    #include <memory>

    int main(void)
    {
        std::shared_ptr<int> currInt(nullptr);

        std::shared_ptr<int> newInt(new int);
        currInt = newInt;

        return 0;
    }

事實證明,這不是有效的C ++ 11(以前是在草稿版本中),並且賦值構造函數現在使用move語義。 我不明白的東西。

有人可以解釋一下我如何修改上面的代碼以使其工作嗎?

有一個拷貝構造函數shared_ptr ,否則什么是點shared_ptr

OP的clang鏈接表示,如果僅定義了移動分配運算符,則將隱式刪除復制構造函數, 從而導致shared_ptr行為不正確 這也可以在Boost變更集中看到,在該變更集中顯式添加了副本分配運算符以更正錯誤。

您可以在第20.7.2.2.3 [util.smartptr.shared.assign] / 1–3中找到shared_ptr的副本分配運算符。

shared_ptr& operator=(const shared_ptr& r) noexcept;
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM