Created
October 20, 2019 17:07
-
-
Save monoid/9365573dbecbc50d62edb95c2857c9bd to your computer and use it in GitHub Desktop.
C++ inset is not a replacement for Rust Element API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <functional> | |
| #include <unordered_map> | |
| class HiMike { | |
| private: | |
| int value; | |
| public: | |
| HiMike(int value) : value(value) { } | |
| friend class std::hash<HiMike>; | |
| friend bool operator==(const HiMike&, const HiMike&); | |
| }; | |
| bool operator==(const HiMike& a, const HiMike& b) { | |
| return a.value == b.value; | |
| } | |
| namespace std { | |
| template<> | |
| class hash<HiMike> { | |
| public: | |
| typedef HiMike argument_type; | |
| typedef size_t result_type; | |
| result_type operator()(const argument_type& hi) const { | |
| cerr << "Hash for " << hi.value << endl; | |
| return hash<int>{}(hi.value); | |
| } | |
| }; | |
| } | |
| int main() { | |
| std::unordered_map<HiMike, int> hihash; | |
| hihash[HiMike(0)] = 42; | |
| HiMike odin{1}; | |
| auto it = hihash.find(odin); | |
| std::cerr << "sizeof=" << sizeof(it) << ", it is end: " << (it == hihash.end()) << std::endl; | |
| hihash.insert(it, {odin, 2}); | |
| std::cerr << hihash[odin] << std::endl; | |
| return 0; | |
| } | |
| /* | |
| ib@monoid:/tmp$ clang++-6.0 -std=c++17 -O3 test.cpp | |
| ib@monoid:/tmp$ ./a.out | |
| Hash for 0 | |
| Hash for 1 | |
| sizeof=8, it is end: 1 | |
| Hash for 1 | |
| Hash for 1 | |
| 2 | |
| ib@monoid:/tmp$ g++ --std=c++17 -O3 test.cpp | |
| ib@monoid:/tmp$ ./a.out | |
| Hash for 0 | |
| Hash for 1 | |
| sizeof=8, it is end: 1 | |
| Hash for 1 | |
| Hash for 1 | |
| 2 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment