Last active
August 29, 2015 14:27
-
-
Save jgoday/d53b36bbc65fee711f99 to your computer and use it in GitHub Desktop.
lambda and variable arguments in c++11
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 <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