Should the Copy-and-Swap Idiom become the Copy-and-Move Idiom in C++11?

后端 未结 3 755
甜味超标
甜味超标 2020-12-01 08:05

As explained in this answer, the copy-and-swap idiom is implemented as follows:

class MyClass
{
private:
    BigClass data;
    UnmovableClass *dataPtr;

pub         


        
3条回答
  •  悲&欢浪女
    2020-12-01 08:28

    Give each special member the tender loving care it deserves, and try to default them as much as possible:

    class MyClass
    {
    private:
        BigClass data;
        std::unique_ptr dataPtr;
    
    public:
        MyClass() = default;
        ~MyClass() = default;
        MyClass(const MyClass& other)
            : data(other.data)
            , dataPtr(other.dataPtr ? new UnmovableClass(*other.dataPtr)
                                    : nullptr)
            { }
        MyClass& operator=(const MyClass& other)
        {
            if (this != &other)
            {
                data = other.data;
                dataPtr.reset(other.dataPtr ? new UnmovableClass(*other.dataPtr)
                                            : nullptr);
            }
            return *this;
        }
        MyClass(MyClass&&) = default;
        MyClass& operator=(MyClass&&) = default;
    
        friend void swap(MyClass& first, MyClass& second)
        {
            using std::swap;
            swap(first.data, second.data);
            swap(first.dataPtr, second.dataPtr);
        }
    };
    

    The destructor could be implicitly defaulted above if desired. Everything else needs to be explicitly defined or defaulted for this example.

    Reference: http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf

    The copy/swap idiom will likely cost you performance (see the slides). For example ever wonder why high performance / often used std::types like std::vector and std::string don't use copy/swap? Poor performance is the reason. If BigClass contains any std::vectors or std::strings (which seems likely), your best bet is to call their special members from your special members. The above is how to do that.

    If you need strong exception safety on the assignment, see the slides for how to offer that in addition to performance (search for "strong_assign").

提交回复
热议问题