std::string with stateful Allocator

226 views Asked by At

I have a allocator which allocates memory based on its instance variable MemoryContext. I tried to create a std::string for this kind of allocator. Header looks like this

class String {
private:
    std::basic_string< char
                        , std::char_traits<char>
                        , MemoryContextAllocator<char> >  my_string;

public:
    explicit String( MemoryContext* memoryContext );
    ~String();
    MemoryContextAllocator<char> getAllocator() const;

};

Source looks like this

String::String(MemoryContext* memoryContext)
:my_string( MemoryContextAllocator<char>(memoryContext) )
{
}

String::~String() {
}

During compilation, i get an error like this

/usr/include/c++/4.8/bits/basic_string.tcc:156:27: error: no matching function for call to ‘MemoryContextAllocator<char>::MemoryContextAllocator()

It is trying to call the constructor of the allocator with no arguments. If i don't want to introduce this default constructor and use setters, how can i solve this issue?

0

There are 0 answers