mercredi 7 août 2019

Create a std::vector of type having private constructor

I have a class whose constructors are private. I need to create a std::vector of instances of this class. Now, I am providing my own allocator for this purpose and the class has the allocator as a friend. However on MSVS 2015 with C++11, I get the error as:

error C2664: 'const MyClass*std::_Wrap_alloc<MyClass::MyClassAllocator>::address(const MyClass&) const': cannot convert argument 1 from 'std::_Container_proxy' to 'MyClass&'

Here is my allocator:

struct MyClassAllocator: std::allocator<MyClass>
{
    template <class U, class... Args>
    void construct(U* ptr, Args&&... args)
    {
        ::new(reinterpret_cast<void*>(ptr)) U(std::forward<Args>(args)...);
    }

    template <class U> struct rebind { typedef MyClassAllocator other; };
};

This allocator is taken from this SO answer.

What am I doing wrong here?

Aucun commentaire:

Enregistrer un commentaire