Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- void func()
- {
- std::cout << "Hello";
- }
- auto t = []{ func(); };
- typedef decltype(t) functor_type;
- template <class F>
- void functor_caller()
- {
- F f;
- f();
- }
- int main()
- {
- functor_caller<functor_type>();
- return 0;
- }
- error: use of deleted function '<lambda()>::<lambda>()'
- error: a lambda closure type has a deleted default constructor
- #define WRAP_FUNC(f)
- struct f##_functor
- {
- template <class... Args >
- auto operator()(Args ... args) ->decltype(f(args...))
- {
- return f(args...);
- }
- };
- WRAP_FUNC(func);
- functor_caller<func_functor>()
- {
- int n = 0;
- auto t = [&n](int a) -> int { return n += a; };
- }
- std::function<int(int)> F = t;
- template <typename F>
- int compute(int a, int b, F f)
- {
- return a * f(b); // example
- }
- template<class F>
- struct wrapper
- {
- static_assert(std::is_empty<F>(), "Lambdas must be empty");
- template<class... Ts>
- auto operator()(Ts&&... xs) const -> decltype(reinterpret_cast<const F&>(*this)(std::forward<Ts>(xs)...))
- {
- return reinterpret_cast<const F&>(*this)(std::forward<Ts>(xs)...);
- }
- };
- template <class F>
- void function_caller()
- {
- wrapper<F> f;
- f();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement