アロケータがどのように動作するかを理解しようとしています。しかし、私は、アロケータを受け入れる簡単なコンテナを実装しようとすると問題に遭遇します。今私はこれで終わった場合:それは私にそのエラーを修正する方法エラーmember function 'allocate' not viable: 'this' argument has type 'const allocator_type' (aka 'const std::__1::allocator<int>'), but function is not marked const
簡単なアロケータ対応コンテナ?
を与える
template<class T, class Allocator =std::allocator<T>> class Container {
public:
using allocator_type = Allocator;
using value_type = T;
using pointer = typename std::allocator_traits<allocator_type>::pointer;
using reference = value_type&;
using size_type = std::size_t;
Container(size_type n =0 , const allocator_type& allocator =allocator_type()){
std::cout << "ctor" << std::endl;
allocator.allocate(n);
};
};
int main(int argc, const char* argv[]){
Container<int> c {5};
return 0;
}
、してください?何か不足していますか? 私は後で形質を使うつもりですが、古いものを最初に使って機能させたいと思います。 const
方法として定義されていないallocator
のallocate
メソッドを呼び出す
それは、関数ではありませんが、 'const'とマークされた' allocator'です。 libC++のやり方を見てみると、libstdC++はこれを 'std :: vector'のように実装しています。 – NathanOliver
標準コンテナは、通常、このconst参照を使用して、独自のメンバ(または基本クラス)を初期化し、このメンバに対してallocateを呼び出すことができます。 – SergeyA