From bb53f18dfdfc6013237468dfabd92af4547346fd Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Tue, 10 Oct 2023 02:23:22 -0400 Subject: [PATCH] This is just the rudiments of a more comprehensive `function_traits` design. It's good for many common cases, but it should handle member functions and discriminate between `std::function` objects and builtins. It's mostly just boilerplate to do that. --- function_traits.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 function_traits.h diff --git a/function_traits.h b/function_traits.h new file mode 100644 index 0000000..736104e --- /dev/null +++ b/function_traits.h @@ -0,0 +1,47 @@ +static_assert( __cplusplus > 2020'00 ); + +#pragma once + +#include +#include +#include + +namespace Alepha::inline Cavorite ::detail:: function_traits_module +{ + inline namespace exports + { + template< typename Function > struct function_traits; + + template< typename Function > + using get_args_t= typename function_traits< Function >::args_type; + + template< typename Function, int index > + using get_arg_t= std::decay_t< std::tuple_element_t< index, get_args_t< Function > > >; + } + + // General catchall... + template< typename Function > + struct exports::function_traits + : exports::function_traits< std::decay_t< decltype( std::function( std::declval< Function >() ) ) > > + {}; + + template< typename Result, typename ... Args, bool noexceptness > + struct exports::function_traits< std::function< Result ( Args... ) noexcept( noexceptness ) > > + { + static constexpr bool noexcepted= noexceptness; + using function_type= std::function< Result ( Args... ) noexcept( noexceptness ) >; + using member_type= std::false_type; + using const_member_type= std::false_type; + + using args_type= std::tuple< Args... >; + static constexpr std::size_t args_size= sizeof...( Args ); + + using return_type= Result; + using signature_type= Result( Args... ); + }; +} + +namespace Alepha::Cavorite::inline exports::inline function_traits_module +{ + using namespace detail::function_traits_module::exports; +}