fork(2) download
  1. #include <algorithm>
  2. #include <list>
  3. #include <vector>
  4.  
  5. template <typename ContainerOut, typename ContainerIn>
  6. ContainerOut KeepNegatives(const ContainerIn& xs)
  7. {
  8. ContainerOut result;
  9. auto itOut = std::inserter(result, std::end(result));
  10. auto isNegative = [](auto x){ return x < 0; };
  11. std::copy_if(std::begin(xs), std::end(xs), itOut, isNegative);
  12. return result;
  13. }
  14.  
  15. int main()
  16. {
  17. typedef std::vector<int> IntVector;
  18. typedef std::list<int> IntList;
  19.  
  20. IntVector intVector = { 1, -2, -3, 4 };
  21. IntList intList = { 1, -2, -3, 4 };
  22.  
  23. auto intVec2 = KeepNegatives<IntVector>(intList);
  24. auto intList2 = KeepNegatives<IntList>(intVector);
  25. auto intVec3 = KeepNegatives<IntVector>(intVector);
  26.  
  27. auto intVec4 = KeepNegatives(intVector);
  28. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:27:43: error: no matching function for call to 'KeepNegatives(IntVector&)'
     auto intVec4 = KeepNegatives(intVector);
                                           ^
prog.cpp:6:14: note: candidate: template<class ContainerOut, class ContainerIn> ContainerOut KeepNegatives(const ContainerIn&)
 ContainerOut KeepNegatives(const ContainerIn& xs)
              ^
prog.cpp:6:14: note:   template argument deduction/substitution failed:
prog.cpp:27:43: note:   couldn't deduce template parameter 'ContainerOut'
     auto intVec4 = KeepNegatives(intVector);
                                           ^
stdout
Standard output is empty