Created
April 15, 2017 12:27
-
-
Save hryniuk/2097cca7a916e20bf469500a35dde53a to your computer and use it in GitHub Desktop.
Function template for mapping values with a given functor
This file contains hidden or 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 <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