Re: [boost] [JSON[review] Andrzej's review
Vinnie Falco wrote:
Just open an issue and explain how you think self-swap, self-move, and self-assignment should work and I will make the changes.
They should be no-ops.
More specifically, self-swap should be a no-op, and assignments should be equivalent to: T& operator=( T const& rhs ) { T(rhs).swap(*this); return *this; } T& operator=( T&& rhs ) { T(std::move(rhs)).swap(*this); return *this; } This is important for classes such as json::value, object, array, where *this may own rhs, so it's important not to destroy rhs too early. E.g. v = v.at(0); v = std::move(v.at(0)); a = a.at(0).as_array(); a = std::move(a.at(0)).as_array(); o = o.at("key").as_object(); o = std::move(o.at("key").as_object()); This is not self-assignment, but it has the same lifetime pitfalls - if you destroy the old contents of *this, rhs is invalidated, so you can no longer copy (or move) it.
On Thu, Sep 17, 2020 at 2:03 PM Peter Dimov via Boost
T& operator=( T const& rhs ) { T(rhs).swap(*this); return *this; }
The memory_resource associated with a value can never change after construction, so the above would have to be written as: T& operator=( T const& rhs ) { T( rhs, this->storage() ).swap( *this ); return *this; } and T& operator=( T const& rhs ) { T( std::move(rhs), this->storage() ).swap( *this ); return *this; } Which I think is ok... Thanks
participants (2)
-
Peter Dimov
-
Vinnie Falco