如何使用propagate_on_container_copy/move_assignment::value?

问题描述

晚上好。我注意到 std::vector 对复制和移动赋值运算符有一些要求。我正在尝试使用 (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value 来实现它们。

到目前为止我的实现方式是否正确?

constexpr vector& operator=(vector&& other) noexcept 
        {
            if (this != &other)
            {
                if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) 
                {
                    destruct(m_size); 
                    deallocate(m_capacity);
                    m_allocator = std::move(other.m_allocator);
                    m_vector = other.m_vector;
                }
                else if (m_allocator == other.m_allocator) 
                {
                    destruct(m_size);
                    deallocate(m_capacity);
                    m_vector = other.m_vector;
                }
                else
                {
                    destruct(m_size);
                    deallocate(m_capacity);
                    allocate(other.m_capacity);
                    std::uninitialized_move(other.m_vector,other.m_vector + other.m_size,m_vector);
                }
                m_size = other.m_size;
                m_capacity = other.m_capacity;
                reset(other);
            }
            return *this;
        }

destruct(m_size) = 在循环中使用 std::allocator_traits::destroy 来销毁任何已构造的东西,

deallocate(m_capacity) = 使用 std::allocator_traits<:deallocate>

m_allocator = 它是分配器类型,它是传递给模板的分配器,认情况下是 std::allocator

reset(other) = 使另一个向量保持良好状态 (m_vector nullptr,capacity & size = 0)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)