如何用我自己的替换std :: basic_string的分配器

时间:2015-04-10 00:51:27

标签: c++ allocator

我写了一个内存分配器,我想用它替换std容器的分配器。我成功地替换了std :: vector,std :: list和std :: set。

在.h文件中:

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>>;

在.cpp文件中:

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

所有编译都运行正常。(你不需要考虑MyAllocatorAdapter的定义和实现,如果发布在这里并且与此问题无关,那就太多了。)

但是当它变成弦乐时,男人,我被困在这里两天了。 这是我的.h文件:

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

它编译,但在.cpp文件中,我写道:

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

然后出现错误消息:

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

请帮助,欢迎提出任何意见!

0 个答案:

没有答案