Last active
December 14, 2017 14:44
-
-
Save eguiraud/2d0fdec9e5b11fb00f3db0cd3214f66a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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