Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <unordered_set>
  2.  
  3. using namespace std;
  4.  
  5. struct MyType {
  6.     int val;
  7. };
  8.  
  9. struct MyTypeEquals {
  10.     using is_transparent = void;
  11.     bool operator()(MyType const& l, MyType const& r) const {
  12.         return l.val == r.val;
  13.     }
  14.     bool operator()(int const l, MyType const& r) const {
  15.         return l == r.val;
  16.     }
  17.     bool operator()(MyType const& l, int const r) const {
  18.         return l.val == r;
  19.     }
  20. };
  21.  
  22. struct MyTypeHash {
  23.     using transparent_key_equal = MyTypeEquals;
  24.  
  25.     std::size_t operator()(MyType const& s) const {
  26.         return hash<int>()(s.val);
  27.     }
  28.  
  29.     std::size_t operator()(int const& s) const {
  30.         return hash<int>()(s);
  31.     }
  32. };
  33.  
  34. int main()
  35. {
  36.     unordered_set<MyType, MyTypeHash, MyTypeEquals> mytype_set;
  37.     mytype_set.insert(MyType{1});
  38.     mytype_set.equal_range(1);
  39.     // As of 2020-02-14 this gives a compile error in GCC 10.0.1.20200 with -std=c++2a:
  40.     // no known conversion for argument 1 from 'int' to 'const key_type&' {aka 'const MyType&'}
  41.     // as
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement