Copy/move constructors

To support the ability to copy and move our container, we will need to implement a copy and move constructor as follows:

    container(
const container &other,
const Allocator &alloc
) :
m_v(other.m_v, alloc)
{
std::cout << "5 ";
}

container(
container &&other
) noexcept :
m_v(std::move(other.m_v))
{
std::cout << "6 ";
}

Since our custom wrapper container must always remain in sorted order, copying or moving one container to another doesn't change the order of the elements in the container, meaning that no sort operation is needed for these constructors either. We do, however, take special care to ensure that a copy and a move occur properly by copying or moving the internal std::vector that our container encapsulates.

For completeness, we also provide a move constructor that allows us, just like the std::vector, to move while providing a custom allocator, as follows:

    container(
container &&other,
const Allocator &alloc
) :
m_v(std::move(other.m_v), alloc)
{
std::cout << "7 ";
}

Next, we will provide a constructor that takes an initializer list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset