Skip to content

Instantly share code, notes, and snippets.

@jgoday
Last active August 29, 2015 14:27
Show Gist options
  • Save jgoday/d53b36bbc65fee711f99 to your computer and use it in GitHub Desktop.
Save jgoday/d53b36bbc65fee711f99 to your computer and use it in GitHub Desktop.
lambda and variable arguments in c++11
#include <functional>
#include <iostream>
template<typename T>
void log(T arg)
{
std::cout << arg;
std::cout << std::endl;
}
template<typename T, typename... Args>
void log(T arg, Args... args)
{
std::cout << arg << " ";
log(args...);
}
template<typename T, typename U>
U map(std::function<U(T)> fn, T value)
{
log("Map function:", value, value);
return fn(value);
}
int main(int, char**)
{
std::function<int(int)> fn = [](int a) { return a + 2; };
std::cout << map(fn, 10) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment