When to call the select_on_container_copy_construction for a container?

  allocator, c++, containers

I’m trying to design an allocator aware container from scratch. The std::allocator_traits class has a member method called select_on_container_copy_construction(..). I saw a question covering the reason for its existence. But, I couldn’t get where I should use this method. Here is a simplified custom container with some notes on it.

/*** Libraries ***/
#include <memory>       // std::allocator, std::allocator_traits

/*** Container Class ***/
template<class T, class Allocator = std::allocator<T>>
class CustomContainer{
public:
    using size_type         = typename std::allocator_traits<Allocator>::size_type;
    using pointer           = typename std::allocator_traits<Allocator>::pointer;

    /*** Constructors and Destructor ***/
    // Default constructor
    CustomContainer() = default;

    // Copy constructor
    CustomContainer(const CustomContainer& other)
        : allocator(std::allocator_traits<Allocator>::select_on_container_copy_construction(other.allocator))
    {
        // Copy construction algorithm here...
    }

    // Move constructor
    CustomContainer(CustomContainer&& other)
        : allocator(/* What to do here? */)
    {
        // Move construction algorithm here...
    }

    /*** Operators ***/
    CustomContainer& operator=(const CustomContainer& other)
    {
        allocator = other.allocator;  // Is that true?

        // Copy assignment algorithm here...
    }

    /*** Member Methods ***/
    // Some member methods here..

private:
    /*** Members ***/
    pointer     data    = nullptr;
    std::size_t sz      = 0;
    Allocator   allocator;
};

As you can see that I called the select_on_container_copy_construction(..) method inside the initialization list of the copy constructor. Are there any other places that I should call this method again? For example, the copy assignment or move constructor methods may need it? How to determine whether I should or not call it?

Source: Windows Questions C++

LEAVE A COMMENT