fork(2) download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <list>
  6. #include <set>
  7. #include <vector>
  8.  
  9. template<typename ContainerOut, typename X, typename Y, typename ContainerIn>
  10. ContainerOut Map( const ContainerIn& xs, const std::function<Y( const X& )>& f )
  11. {
  12. ContainerOut ys;
  13. std::transform( begin( xs ), end( xs ), std::inserter( ys, end( ys ) ), f );
  14. return ys;
  15. }
  16.  
  17. struct Foo {
  18. Foo( int val ) : val_( val ) {}
  19. int val_;
  20. };
  21.  
  22. std::set<int> FooValsToIntSet( const std::list<Foo>& foos )
  23. {
  24. //Map<std::set<int>, Foo, int>
  25. return Map( foos, []( const Foo& foo )
  26. {
  27. return foo.val_;
  28. } );
  29. }
  30.  
  31. int main()
  32. {
  33. std::list<Foo> foos = { 1, 2, 2, 3 };
  34. std::set<int> vals = FooValsToIntSet( foos );
  35. for ( auto& v : vals )
  36. std::cout << v << std::endl;
  37. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'std::set<int> FooValsToIntSet(const std::list<Foo>&)':
prog.cpp:28:4: error: no matching function for call to 'Map(const std::list<Foo>&, FooValsToIntSet(const std::list<Foo>&)::<lambda(const Foo&)>)'
  } );
    ^
prog.cpp:10:14: note: candidate: template<class ContainerOut, class X, class Y, class ContainerIn> ContainerOut Map(const ContainerIn&, const std::function<Y(const X&)>&)
 ContainerOut Map( const ContainerIn& xs, const std::function<Y( const X& )>& f )
              ^
prog.cpp:10:14: note:   template argument deduction/substitution failed:
prog.cpp:28:4: note:   'FooValsToIntSet(const std::list<Foo>&)::<lambda(const Foo&)>' is not derived from 'const std::function<Y(const X&)>'
  } );
    ^
stdout
Standard output is empty