fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4.  
  5. struct Foo
  6. {
  7. int i;
  8. };
  9.  
  10. std::vector<std::reference_wrapper<const Foo>> foos;
  11.  
  12. void store(const Foo& container)
  13. {
  14. foos.push_back(std::cref(container));
  15. }
  16.  
  17. int main()
  18. {
  19.  
  20. Foo f{5};
  21.  
  22. const Foo f1(f);
  23.  
  24. store(f);
  25. store(f);
  26. store(f);
  27. store(f1);
  28.  
  29. for (auto const& f: foos)
  30. {
  31. std::cout << f.get().i << "\n";
  32. }
  33. }
  34.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
5
5
5
5