Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created December 13, 2015 12:14
Show Gist options
  • Select an option

  • Save 2bbb/6ac15ded37a9d0af3058 to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/6ac15ded37a9d0af3058 to your computer and use it in GitHub Desktop.
lambda_template
#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