fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4.  
  5. template<class InputIterator, class Function>
  6. Function my_for_each(InputIterator first, InputIterator last, Function fn)
  7. {
  8. while (first != last) {
  9. fn(*first);
  10. ++first;
  11. }
  12. return fn;
  13. }
  14.  
  15. struct FunctorNMC //non-move-constructible
  16. {
  17. FunctorNMC() = default;
  18. FunctorNMC(FunctorNMC&&) = delete;
  19.  
  20. void operator()(int a) const { std::cout << a << " "; }
  21. };
  22.  
  23. void print(int a) { std::cout << a << " "; }
  24.  
  25. int main()
  26. {
  27. std::vector<int> v{1,2,3};
  28.  
  29. auto lambda = [](int a){ std::cout << a << " "; };
  30. std::function<void(int)> func = lambda;
  31.  
  32. //my_for_each(v.begin(), v.end(), FunctorNMC());
  33. std::cout << "\n";
  34.  
  35. FunctorNMC tmp;
  36. my_for_each(v.begin(), v.end(), tmp);
  37.  
  38. std::cout << "\n";
  39. my_for_each(v.begin(), v.end(), lambda);
  40. std::cout << "\n";
  41. my_for_each(v.begin(), v.end(), func);
  42. std::cout << "\n";
  43. my_for_each(v.begin(), v.end(), print);
  44. std::cout << "\n";
  45. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:36:37: error: use of deleted function ‘constexpr FunctorNMC::FunctorNMC(const FunctorNMC&)’
  my_for_each(v.begin(), v.end(), tmp);
                                     ^
prog.cpp:15:8: note: ‘constexpr FunctorNMC::FunctorNMC(const FunctorNMC&)’ is implicitly declared as deleted because ‘FunctorNMC’ declares a move constructor or move assignment operator
 struct FunctorNMC //non-move-constructible
        ^
prog.cpp:6:10: error:   initializing argument 3 of ‘Function my_for_each(InputIterator, InputIterator, Function) [with InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; Function = FunctorNMC]’
 Function my_for_each(InputIterator first, InputIterator last, Function fn)
          ^
prog.cpp: In instantiation of ‘Function my_for_each(InputIterator, InputIterator, Function) [with InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; Function = FunctorNMC]’:
prog.cpp:36:37:   required from here
prog.cpp:12:9: error: use of deleted function ‘FunctorNMC::FunctorNMC(FunctorNMC&&)’
  return fn;  
         ^
prog.cpp:18:2: error: declared here
  FunctorNMC(FunctorNMC&&) = delete;
  ^
stdout
Standard output is empty