fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <functional>
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. unique_ptr<int> uptr = make_unique<int>(5566);
  9. std::function<void()> func;
  10. // 1. Why compile error...
  11. // func = std::bind([](unique_ptr<int> &&uptr2) {cout << *uptr2 << endl; }, std::move(uptr));
  12. // func();
  13. // 2. Why can I only write auto for deduction?
  14. auto func2 = std::bind([](const unique_ptr<int>& uptr2/*no const is OK but why?*/)
  15. { cout << *uptr2 << endl; },
  16. std::move(uptr));
  17. // only can use "const &" to invoke func2?
  18. // && or by value will compile error, but "&" can run successfully without reason....
  19. func2();
  20. auto func3 = std::bind([](unique_ptr<int> uptr2) {cout << *uptr2 << endl; }, std::move(uptr));
  21. //func3(); // 3. How to invoke func3?... compile error
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
5566