Skip to content

Instantly share code, notes, and snippets.

@eguiraud
Last active December 14, 2017 14:44
Show Gist options
  • Save eguiraud/2d0fdec9e5b11fb00f3db0cd3214f66a to your computer and use it in GitHub Desktop.
Save eguiraud/2d0fdec9e5b11fb00f3db0cd3214f66a to your computer and use it in GitHub Desktop.
#include <vector>
#include <functional>
template<typename R, typename T>
std::function<std::vector<R>(std::vector<T>&)>
ApplyToVec(R(T::*m)()) {
auto applyToEach = [m](std::vector<T> &v) {
std::vector<R> r;
r.reserve(v.size());
for (auto &e : v) {
r.emplace_back((e.*m)());
}
return r;
};
return applyToEach;
}
class A {
public:
int foo() { return 42; };
};
int main() {
std::vector<A> v(3);
auto r = ApplyToVec(&A::foo)(v);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment