簡體   English   中英

復制和交換習語警告:在所有控制路徑上遞歸,函數將導致運行時堆棧溢出

[英]Copy&Swap idiom warning : recursive on all control paths, function will cause runtime stack overflow

這是我第一次實際使用Copy&Swap成語。 但是我在匯編時得到了這個警告,使用Microsoft VS2013:

警告C4717:'std :: swap':在所有控制路徑上遞歸,函數將導致運行時堆棧溢出

我試圖得到有罪遞歸發生的時間/地點,但我無法掌握它。 我做錯了什么。 什么可以糾正?

class A
{
private:
    SmartPtr m_sp = nullptr;

public:
    A(){}

    ~A(){}

    A(const A& other)
    {       
        m_sp = other.m_sp;    
    }

    A(A&& other)
    {
        std::swap(*this, other);
    }

    A& operator=(A other)
    {
        std::swap(other, *this);
        return *this;
    }
}

您需要實現自己的交換功能。 std::swap將調用賦值運算符。 這將調用std::swap 這將調用賦值運算符。 哪個會......

class A
{
    // as before

    swap(A& other) { std::swap(m_sp, other.m_sp); }
    A& operator=(A other)
    {
        swap(other);
        return *this;
    }
};

暫無
暫無

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

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