電子書籍の厳選無料作品が豊富!

こんにちは。
質問させてください。

現在VisualStudio2008 Academic EditionでC++のプログラムを書いています。
vectorで使用可能なアロケータを作ったのですが、mapでそのアロケータを利用する方法がわかりません。

// vectorで自作アロケータを利用
std::vector< char, my_allocator<char> > myVec;

// mapで自作アロケータを利用
std::map< int, float, std::less< int >, my_allocator< std::pair< int, float > > > myMap;

vectorの場合はコンパイルできるのですがmapの場合は
error C2664: 'my_allocator<_type>::allocator(const my_allocator<_type> &)' : 1 番目の引数を 'my_allocator<_type>' から 'const my_allocator<_type> &' に変換できません。

というエラーが出てコンパイルできません。
ご教授願います。

A 回答 (2件)

std::map が value_type のための rebind 定義を別途要求するせいだと思われます。



下記のコンストラクタは定義していますか?

my_allocator() throw(){}
my_allocator(const my_allocater<_Ty>&) throw(){}
template <class _Other> my_allocator(const allocator<_Other>&) throw(){}


value_type に key_type と同様のカスタム アロケート処理を行なう場合、空の定義でかまわないようです。


参考までに。
http://www.geocities.jp/ky_webid/cpp/library/028 …
http://monsho.blog63.fc2.com/blog-entry-93.html
    • good
    • 0

std::map< int, float, std::less< int >, my_allocator< std::pair< const int, float > > > myMap;


ではないでしょうか?

ちなみに私の環境はVS2005ですが
std::map< int, float, std::less< int >, my_allocator< std::allocator< int, float > > > myMap;
でコンパイルが通りますが2008だと型に厳しくなっているのかもしれません。

もし、std::map< int, float, std::less< int >, std::allocator< std::pair< const int, float > > > myMap;
でコンパイルが通るならアロケータの実装に問題があるように思います。
ソースはついていると思うのでstd::allocatorのソースとmy_allocatorを比較してみるとよいでしょう。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!