Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save buffyanamin/d1addb7404296c24131fb578197f1184 to your computer and use it in GitHub Desktop.
Save buffyanamin/d1addb7404296c24131fb578197f1184 to your computer and use it in GitHub Desktop.
namespace detail {
template <typename Res, typename... Args>
Obj<Res, Args...> NewObj(std::function<Res(Args...)>&& func) {
return std::make_shared<Obj<Res(Args...)>>(std::forward<std::function<Res(Args...)>&&>(func));
}
}
namespace detail {
template <typename F>
struct function_traits : public function_traits<decltype(&std::decay<F>::type::operator())> {};
template <typename R, typename C, typename... Args>
struct function_traits<R (C::*)(Args...) const> {
using function_type = std::function<R(Args...)>;
};
// support mutable lambda object
template <typename R, typename C, typename... Args>
struct function_traits<R (C::*)(Args...)> {
using function_type = std::function<R(Args...)>;
};
} // namespace detail
template <typename F>
using function_type_t = typename detail::function_traits<F>::function_type;
template <typename F>
function_type_t<F> to_function(F&& lambda) {
return static_cast<function_type_t<F>>(std::forward<F>(lambda));
}
template <typename Closure>
auto NewObj(Closure&& closure) {
return detail::NewObj(to_function(std::forward<Closure>(closure)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment