fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. void fnRef(std::shared_ptr<int>& ptr)
  5. {
  6. auto lambda = [=]() { std::cout << ptr.use_count() << ':' << __PRETTY_FUNCTION__ << '\n'; };
  7. lambda();
  8. }
  9.  
  10. void fnVal(std::shared_ptr<int> ptr)
  11. {
  12. auto lambda = [=]() { std::cout << ptr.use_count() << ':' << __PRETTY_FUNCTION__ << '\n'; };
  13. lambda();
  14. }
  15.  
  16. int main()
  17. {
  18. std::shared_ptr<int> ptr(new int);
  19. for (int i=0; i<10; ++i)
  20. fnVal(ptr);
  21. std::cout << '\n';
  22.  
  23. for (int i=0; i<10; ++i)
  24. fnRef(ptr);
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1
3:fnVal(std::shared_ptr<int>)::__lambda1

2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0
2:fnRef(std::shared_ptr<int>&)::__lambda0