本当にコンテナは、独自の帳簿材料を割り当てるようにアロケータを再利用します。 (それはstd::list
では問題でしょうが、それは一般的には本当のこと*わけではありません。)標準アロケータ要件はrebind
テンプレートの存在を義務付ける理由です:あなたのアロケータがAlloc = my_allocator<T>
ある場合
typedef typename Alloc::template rebind<MyInternalStuff>::other internal_allocator;
を、そしてinternal_allocator
はなりmy_allocator<MyInternalStuff>
。
これは、Electronic ArtsがC++標準ライブラリで持っていた厄介なものの1つだと私は信じています。その理由は、彼らのEASTLライブラリが厳密な制御を提供するアロケータに対して異なる規約を使用している理由です。
*)は、典型的には、各ノードは、いくつかのタイプNode<T>
の1つのモノリシック対象となるので、私はstd::list<T, Alloc>
のみこれまでアロケータとしてAlloc::rebind<Node<T>>::other
を使用すると仮定する。
[複数の編集で申し訳ありません。私は出力が絡み合い、正しく解釈しなかった。私は今、各容器を別々に印刷し、それに応じて出力を固定しました。 std::list
は確かにだけアロケータを必要としない]
更新:をただ笑いのために、私は建設時に、独自の型名を印刷し、ほとんどのdemangle-アロケータを書きました。ここで入力があります:
#include <unordered_map>
#include <set>
#include <deque>
#include <list>
#include <vector>
#include <map>
#include <iostream>
int main()
{
std::cout << "----- unordered_map<int, double> -----------" << std::endl;
std::unordered_map<int, double, std::hash<int>, std::equal_to<int>, funky_allocator<std::pair<const int, double>>> m { {1, 1.2} };
std::cout << "----- set<int> -----------------------------" << std::endl;
std::set<int, std::less<int>, funky_allocator<int>> s;
std::cout << "----- deque<int> ---------------------------" << std::endl;
std::deque<int, funky_allocator<int>> d;
std::cout << "----- list<int> ----------------------------" << std::endl;
std::list<int, funky_allocator<int>> l;
std::cout << "----- vector<int> --------------------------" << std::endl;
std::vector<int, funky_allocator<int>> c;
std::cout << "----- map<int, bool> -----------------------" << std::endl;
std::map<int, bool, std::less<int>, funky_allocator<std::pair<const int, bool>>> n { { 1, true } };
}
そして、ここで出力:
----- unordered_map<int, double> -----------
Default-construct: funky_allocator<std::pair<int const, double> >
Copy-construct: funky_allocator<std::__detail::_Hash_node<std::pair<int const, double>, false> >
Copy-construct: funky_allocator<std::__detail::_Hash_node<std::pair<int const, double>, false>*>
----- set<int> -----------------------------
Default-construct: funky_allocator<std::_Rb_tree_node<int> >
----- deque<int> ---------------------------
Default-construct: funky_allocator<int>
Copy-construct: funky_allocator<int*>
----- list<int> ----------------------------
Default-construct: funky_allocator<std::_List_node<int> >
----- vector<int> --------------------------
Default-construct: funky_allocator<int>
----- map<int, bool> -----------------------
Default-construct: funky_allocator<std::_Rb_tree_node<std::pair<int const, bool> > >
詳細が使用されるコンストラクタによって異なり:set
とmap
などのコンテナにのみ、いくつかの呼び出しで「正しい」アロケータを構築するかもしれません、他方では、指定されたアロケータのオブジェクトを最初に構築することができる。どちらの方法でも、指定されたアロケータは2つのコンテナで全く使用されず、のみリバウンドバージョンが使用されます。
選択したツールチェーンに同梱されているSTLまたはC++標準ライブラリ実装を意味しますか?それはどちらですか? –
@ TomalakGeret'kal、何? (質問に+1) – avakar
@avakar STLはもともとsgiプロジェクトでした。それ以来、STLのいくつかの実装(STLportなど)が行われており、そのほとんどが標準ライブラリに追加されていますが、いくつかの変更が加えられています。そのすべてが* "STL" *この質問に影響する可能性のある方法では少し曖昧です。 – dmckee