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