Given any function (or callable) type
Function
, how can I get its all arguments types as a tuple type ?
For example, I need a trait function_traits<Function>::arguments
, where:
int f();
typename function_traits<decltype(f)>::arguments // => gives me std::tuple<>
void g(int);
typename function_traits<decltype(g)>::arguments // => gives me std::tuple<int>
void h(int, int);
typename function_traits<decltype(h)>::arguments // => gives me std::tuple<int, int>
I need to get the size of arguments, fortunately boost already has implemented function_traits<F>::arity
Generate an std::integer_sequence
from 1 to artify, map it to arguments type, but here comes the problem, to map integer_sequence
, I need something like this:
function_traits<F>::arg_type<N> // -> N-th arg_type
but boost only provides this:
function_traits<F>::argN_type
How can I implement function_traits<F>::arg_type<N>
? I can use c++ standard up to c++17
A tuple can be an argument, but only one - it's just a variable of type tuple . In short, functions are built in such a way that they take an arbitrary number of arguments. The * and ** operators are able to unpack tuples/lists/dicts into arguments on one end, and pack them on the other end.
Due to the * prefix on the args variable, all extra arguments passed to the function are stored in args as a tuple.
In Python, you can unpack list , tuple , dict (dictionary) and pass its elements to function as arguments by adding * to list or tuple and ** to dictionary when calling function.
There are two ways to pass arguments to a function: by reference or by value. Modifying an argument that's passed by reference is reflected globally, but modifying an argument that's passed by value is reflected only inside the function.
Something like this:
#include <tuple>
template<typename x_Function> class
function_traits;
// specialization for functions
template<typename x_Result, typename... x_Args> class
function_traits<x_Result (x_Args...)>
{
public: using arguments = ::std::tuple<x_Args...>;
};
usage example:
#include <type_traits>
int foo(int);
using foo_arguments = function_traits<decltype(foo)>::arguments;
static_assert(1 == ::std::tuple_size<foo_arguments>::value);
static_assert(::std::is_same_v<int, ::std::tuple_element<0, foo_arguments>::type>);
online compiler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With