fork download
  1. #include <unordered_map>
  2. #include <memory>
  3.  
  4. template<typename T>
  5. class TestAllocator;
  6.  
  7. template<>
  8. class TestAllocator<void>
  9. {
  10. public:
  11. typedef void * pointer;
  12. typedef const void * const_pointer;
  13. typedef void value_type;
  14. typedef size_t size_type;
  15. typedef ptrdiff_t difference_type;
  16. //typedef T & reference; not possible for void
  17. //typedef const T & const_reference; not possible for void
  18.  
  19. template<typename U>
  20. struct rebind
  21. {
  22. typedef TestAllocator<U> other;
  23. };
  24. };
  25.  
  26. template<typename T>
  27. class TestAllocator
  28. {
  29. public:
  30. typedef T * pointer;
  31. typedef const T * const_pointer;
  32. typedef T & reference;
  33. typedef const T & const_reference;
  34. typedef T value_type;
  35. typedef size_t size_type;
  36. typedef ptrdiff_t difference_type;
  37.  
  38. template<typename U>
  39. struct rebind
  40. {
  41. typedef TestAllocator<U> other;
  42. };
  43.  
  44. TestAllocator() { }
  45. template<typename U>
  46. TestAllocator(const TestAllocator<U> &other) { }
  47.  
  48. pointer allocate(int n, pointer hint=0) { return new T[n]; }
  49. pointer deallocate(pointer p, size_t n) { delete[] p; }
  50. pointer address(reference x) { return &x; } // hack! should be std::addressof(x);
  51. void construct(pointer p, const_reference val) { new (p) value_type(val); }
  52. void destroy(pointer p) { p->~value_type(); }
  53. };
  54.  
  55. struct Foo
  56. {
  57. int a, b;
  58. bool operator <(const Foo &r) const
  59. {
  60. return a < r.a;
  61. }
  62. };
  63.  
  64. struct Bar { float a, b; };
  65.  
  66. struct TrivialHash
  67. {
  68. size_t operator()(const Foo &a) const
  69. {
  70. return a.a;
  71. }
  72. };
  73.  
  74. struct TrivialEqual
  75. {
  76. bool operator()(const Foo &a, const Foo &b) const
  77. {
  78. return a.a == b.a;
  79. }
  80. };
  81.  
  82. typedef std::unordered_map<
  83. Foo, Bar,
  84. TrivialHash,
  85. TrivialEqual,
  86. TestAllocator<int>
  87. > TestContainer;
  88.  
  89. int main()
  90. {
  91. TestContainer x;
  92. x.insert(TestContainer::value_type(Foo(), Bar()));
  93. return 0;
  94. }
  95.  
Success #stdin #stdout 0s 3056KB
stdin
Standard input is empty
stdout
Standard output is empty