2

I wrote a memory allocator, and I want to replace std container's allocator with it. I had successfully replaced std::vector, std::list, and std::set.

In .h file:

template <typename T>
using MyVector = std::vector<T, MyAllocatorAdapter<T>>;

template <typename T>
using MyList = std::list<T, MyAllocatorAdapter<T>>;

template <typename T, typename Comparator = std::less<T>>
using MySet = std::set<T, Comparator, MyAllocatorAdapter<T>>;

And in .cpp file:

  ...initiate allocator...
  MyVector<int> my_vector(allocator.Adapter());
  MapleList<float> my_list(allocator.Adapter());

All compile fine and run through.(You don't need to consider the definition and implementation of MyAllocatorAdapter, it's too many if posted here and irrelevant to this problem).

But when it goes to string, man, I was stuck here for two days. Here is my .h file:

template<typename T>
using MyString = std::basic_string<char, std::char_traits<char>, MyAllocatorAdapter<T>> ;

It compiles, but in .cpp file, I write:

MyString<char> myString(allocator.Adapter());

Then there comes the error messages:

In file included from /usr/include/c++/4.8/string:53:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from MP.h:7,
                 from MPTest.cpp:1:
/usr/include/c++/4.8/bits/basic_string.tcc: In instantiation of ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(std::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = MyAllocatorAdapter<char>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/4.8/bits/basic_string.tcc:179:58:   required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = MyAllocatorAdapter<char>]’
MPTest.cpp:496:49:   required from here
/usr/include/c++/4.8/bits/basic_string.tcc:156:27: error: no matching function for call to ‘MyAllocatorAdapter<char>::MyAllocatorAdapter()’
       if (__n == 0 && __a == _Alloc())
                           ^
/usr/include/c++/4.8/bits/basic_string.tcc:156:27: note: candidates are:
In file included from MPTest.cpp:3:0:
MPAllocator.h:142:3: note: MyAllocatorAdapter<T>::MyAllocatorAdapter(const MyAllocatorAdapter<T>&) [with T = char]
   MyAllocatorAdapter(const MyAllocatorAdapter& other) = default;
   ^
MPAllocator.h:142:3: note:   candidate expects 1 argument, 0 provided
MPAllocator.h:138:3: note: template<class U> MyAllocatorAdapter<T>::MyAllocatorAdapter(const MyAllocatorAdapter<U>&)
   MyAllocatorAdapter(const MyAllocatorAdapter<U>& other) 
   ^
MPAllocator.h:138:3: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/string:53:0,
                 from /usr/include/c++/4.8/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8/bits/ios_base.h:41,
                 from /usr/include/c++/4.8/ios:42,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from MP.h:7,
                 from MPTest.cpp:1:
/usr/include/c++/4.8/bits/basic_string.tcc:156:27: note:   candidate expects 1 argument, 0 provided
       if (__n == 0 && __a == _Alloc())
                           ^
In file included from MPTest.cpp:3:0:
MPAllocator.h:133:12: note: MyAllocatorAdapter<T>::MyAllocatorAdapter(MyAllocator*) [with T = char]
   explicit MyAllocatorAdapter(MyAllocator* arena_allocator)
            ^
MPAllocator.h:133:12: note:   candidate expects 1 argument, 0 provided
make[1]: *** [MPTest.o] Error 1
make: *** [src/Mempool] Error 2

Please help, any comments are welcome!

Harvey Dent
  • 327
  • 2
  • 14

0 Answers0