Skip to content

Deduction guides #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions include/function2/function2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1779,20 +1779,70 @@ using function_base = detail::function<
detail::config<IsOwning, IsCopyable, Capacity>,
detail::property<IsThrowing, HasStrongExceptGuarantee, Signatures...>>;

/// An owning copyable function wrapper for arbitrary callable types.
/// An owning copyable function wrapper.
template <typename... Signatures>
using function = function_base<true, true, capacity_default, //
true, false, Signatures...>;
struct function : public function_base<true, true, capacity_default, true, false, Signatures...> {
using base = function_base<true, true, capacity_default, true, false, Signatures...>;
using base::base;
};

/// An owning non copyable function wrapper for arbitrary callable types.
/// An owning non-copyable function wrapper.
template <typename... Signatures>
using unique_function = function_base<true, false, capacity_default, //
true, false, Signatures...>;
struct unique_function : public function_base<true, false, capacity_default, true, false, Signatures...> {
using base = function_base<true, false, capacity_default, true, false, Signatures...>;
using base::base;
};

/// A non owning copyable function wrapper for arbitrary callable types.
/// A non-owning copyable function wrapper.
template <typename... Signatures>
using function_view = function_base<false, true, capacity_default, //
true, false, Signatures...>;
struct function_view : public function_base<false, true, capacity_default, true, false, Signatures...> {
using base = function_base<false, true, capacity_default, true, false, Signatures...>;
using base::base;
};

// Helper to decompose member function pointer types
template <typename T>
struct function_traits;

template <typename R, typename C, typename... Args>
struct function_traits<R (C::*)(Args...)> {
using signature = R(Args...);
};

template <typename R, typename C, typename... Args>
struct function_traits<R (C::*)(Args...) const> {
using signature = R(Args...);
};

// Deduce the signature for a callable F
template <typename F, typename = void>
struct deduce_signature {};

// Handle function pointers
template <typename R, typename... Args>
struct deduce_signature<R(*)(Args...), void> {
using type = R(Args...);
};

// Handle functors (lambdas, objects with operator())
template <typename F>
struct deduce_signature<F, std::void_t<decltype(&F::operator())>> {
private:
using call_operator_t = decltype(&F::operator());
using traits = function_traits<call_operator_t>;
public:
using type = typename traits::signature;
};

// Deduction guides for each wrapper
template <typename F>
function(F) -> function<typename deduce_signature<F>::type>;

template <typename F>
unique_function(F) -> unique_function<typename deduce_signature<F>::type>;

template <typename F>
function_view(F) -> function_view<typename deduce_signature<F>::type>;

#if !defined(FU2_HAS_DISABLED_EXCEPTIONS)
/// Exception type that is thrown when invoking empty function objects
Expand Down