Skip to content

Instantly share code, notes, and snippets.

@hryniuk
Created April 15, 2017 12:27
Show Gist options
  • Save hryniuk/2097cca7a916e20bf469500a35dde53a to your computer and use it in GitHub Desktop.
Save hryniuk/2097cca7a916e20bf469500a35dde53a to your computer and use it in GitHub Desktop.
Function template for mapping values with a given functor
#include <iostream>
#include <functional>
#include <vector>
template<typename Container, typename Function>
void f(Container&& c, Function&& f)
{
for (const auto& e : c) {
f(e);
}
}
auto l = [](auto&& v){std::cout << v << std::endl;};
struct m
{
template<typename T>
void operator()(T&& a)
{
std::cout << a << std::endl;
}
};
template<typename T>
void n(T a)
{
std::cout << a << std::endl;
}
int main()
{
std::vector<int> v{1,2,3,4,5};
f(v, &n<int>);
f(v, l);
f(v, m());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment