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<void>
  87. > TestContainer;
  88.  
  89. int main()
  90. {
  91. TestContainer x;
  92. x.insert(TestContainer::value_type(Foo(), Bar()));
  93. return 0;
  94. }
  95.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/unordered_map:45:0,
                 from prog.cpp:1:
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/hashtable.h: In instantiation of 'std::_Hashtable<Foo, std::pair<const Foo, Bar>, TestAllocator<void>, std::_Select1st<std::pair<const Foo, Bar> >, TrivialEqual, TrivialHash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unordered_map.h:49:5:   instantiated from 'std::__unordered_map<Foo, Bar, TrivialHash, TrivialEqual, TestAllocator<void>, false>'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/unordered_map.h:222:5:   instantiated from 'std::unordered_map<Foo, Bar, TrivialHash, TrivialEqual, TestAllocator<void> >'
prog.cpp:91:19:   instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/hashtable.h:140:59: error: no type named 'reference' in 'class TestAllocator<void>'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/hashtable.h:141:59: error: no type named 'const_reference' in 'class TestAllocator<void>'
stdout
Standard output is empty