#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <set>
#include <vector>
template<typename ContainerOut, typename X, typename Y, typename ContainerIn>
ContainerOut Map( const ContainerIn& xs, const std::function<Y( const X& )>& f )
{
ContainerOut ys;
std::transform( begin( xs ), end( xs ), std::inserter( ys, end( ys ) ), f );
return ys;
}
struct Foo {
Foo( int val ) : val_( val ) {}
int val_;
};
std::set<int> FooValsToIntSet( const std::list<Foo>& foos )
{
//Map<std::set<int>, Foo, int>
return Map( foos, []( const Foo& foo )
{
return foo.val_;
} );
}
int main()
{
std::list<Foo> foos = { 1, 2, 2, 3 };
std::set<int> vals = FooValsToIntSet( foos );
for ( auto& v : vals )
std::cout << v << std::endl;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGl0ZXJhdG9yPgojaW5jbHVkZSA8bGlzdD4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPHZlY3Rvcj4KCnRlbXBsYXRlPHR5cGVuYW1lIENvbnRhaW5lck91dCwgdHlwZW5hbWUgWCwgdHlwZW5hbWUgWSwgdHlwZW5hbWUgQ29udGFpbmVySW4+CkNvbnRhaW5lck91dCBNYXAoIGNvbnN0IENvbnRhaW5lckluJiB4cywgY29uc3Qgc3RkOjpmdW5jdGlvbjxZKCBjb25zdCBYJiApPiYgZiApCnsKCUNvbnRhaW5lck91dCB5czsKCXN0ZDo6dHJhbnNmb3JtKCBiZWdpbiggeHMgKSwgZW5kKCB4cyApLCBzdGQ6Omluc2VydGVyKCB5cywgZW5kKCB5cyApICksIGYgKTsKCXJldHVybiB5czsKfQoKc3RydWN0IEZvbyB7CglGb28oIGludCB2YWwgKSA6IHZhbF8oIHZhbCApIHt9CglpbnQgdmFsXzsKfTsKCnN0ZDo6c2V0PGludD4gRm9vVmFsc1RvSW50U2V0KCBjb25zdCBzdGQ6Omxpc3Q8Rm9vPiYgZm9vcyApCnsKCS8vTWFwPHN0ZDo6c2V0PGludD4sIEZvbywgaW50PgoJcmV0dXJuIE1hcCggZm9vcywgW10oIGNvbnN0IEZvbyYgZm9vICkKCXsKCQlyZXR1cm4gZm9vLnZhbF87Cgl9ICk7Cn0KCmludCBtYWluKCkKewoJc3RkOjpsaXN0PEZvbz4gZm9vcyA9IHsgMSwgMiwgMiwgMyB9OwoJc3RkOjpzZXQ8aW50PiB2YWxzID0gRm9vVmFsc1RvSW50U2V0KCBmb29zICk7Cglmb3IgKCBhdXRvJiB2IDogdmFscyApCgkJc3RkOjpjb3V0IDw8IHYgPDwgc3RkOjplbmRsOwp9
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&)>'
} );
^