fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <unordered_map>
  4.  
  5. using namespace std;
  6.  
  7. class A {
  8. public:
  9. A(unordered_map<int, int>& m) : m(m) {
  10. }
  11. void Start() {
  12. auto t = std::thread(test1, std::ref(m));
  13. t.join();
  14. }
  15. private:
  16. static void test1(unordered_map<int, int>& m) { // complie failed, if the ref change to value(remove &), it's ok, thats confused me
  17. for (auto i : m) {
  18. cout << i.first << i.second << endl;
  19. }
  20. }
  21. unordered_map<int, int> & m;
  22. };
  23.  
  24. int main() {
  25. unordered_map<int, int> m = {{1,2}, {3,4}};
  26. A a(m);
  27. a.Start();
  28. }
  29.  
  30.  
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
34
12