fork(2) download
  1. #include <functional>
  2. #include <memory>
  3. #include <utility>
  4. #include <iostream>
  5.  
  6. template <typename T, typename... Args>
  7. std::unique_ptr<T> make_unique(Args&&... args) {
  8. return std::unique_ptr<T>{new T{std::forward<Args>(args)...}};
  9. }
  10.  
  11. template <typename T>
  12. class Movable {
  13. T value;
  14. public:
  15. explicit Movable(T value) :
  16. value{std::move(value)} {}
  17. ~Movable() { std::cout << "~Movable()" << std::endl; }
  18. Movable(const Movable&) = delete;
  19. Movable& operator = (const Movable&) = delete;
  20. friend std::ostream& operator << (std::ostream& os, const Movable& m) {
  21. return os << m.value;
  22. }
  23. };
  24.  
  25. int main() {
  26. using namespace std::placeholders;
  27. auto f = std::bind([](std::unique_ptr<Movable<int>>& p, int i, const char* s){
  28. std::cout << *p << ' ' << i << ' ' << s << std::endl;
  29. }, make_unique<Movable<int>>(42), _1, _2);
  30. f(13, "Hello, ");
  31. f(69, "World!");
  32. }
  33.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
42 13 Hello, 
42 69 World!
~Movable()