Created
December 13, 2015 12:14
-
-
Save 2bbb/6ac15ded37a9d0af3058 to your computer and use it in GitHub Desktop.
lambda_template
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 <tuple> | |
| namespace bbb { | |
| template <typename T> | |
| struct function_info : public function_info<decltype(&T::operator())> {}; | |
| template <typename class_type, typename ret, typename ... arguments> | |
| struct function_info<ret(class_type::*)(arguments ...) const> { | |
| using function_type = std::function<ret(arguments ...)>; | |
| }; | |
| }; | |
| template <typename res, typename ... args> | |
| void foo(std::function<res(args ...)> f) { | |
| std::cout << "yeah!! res: " << typeid(res).name() << ", args: " << typeid(std::tuple<args ...>).name() << std::endl; | |
| } | |
| void foo(std::function<int(int)> f) { | |
| std::cout << "yeah!! call: " << f(1) << std::endl; | |
| } | |
| template <typename fun> | |
| void foo(fun f) { | |
| std::cout << "adaptor" << std::endl; | |
| foo(static_cast<typename bbb::function_info<fun>::function_type>(f)); | |
| } | |
| int main(int argc, char *argv[]) { | |
| foo(static_cast<std::function<int(int)>>([](int) { return 3; })); | |
| foo([](int) { return 3; }); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment