Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack an array of arguments for a function call

Is it possible to pass a function and a list of its arguments to another function and call it from inside later on?

void testA(int, float, char* ) {}
void testB(int, float, double ) {} 
void testC(MyClass, float, double ) {} 

template <class T>
void applyA(void(*foo)(void*), std::initializer_list<T> args)
{
    foo(/*unpack args somehow*/);
}

template <class T>
void applyB(void(*foo)(void*), std::initializer_list<T> args)
{
    MyClass cls;
    foo(cls, /*unpack the rest of args*/);
}

int main()
{
    applyA(testA, {5, 0.5f, "abc"});
    applyA(testB, {5, 0.5f, 1.5});
    applyB(testC, {0.5f, 1.5});
}
like image 261
WrathOfFlame Avatar asked Feb 07 '18 05:02

WrathOfFlame


1 Answers

You can just forward the arguments, without using arrays, or use tuples like std::apply().

#include <vector>

class MyClass {};

void testA(int, float, const char* ) {}
void testB(int, float, double ) {} 
void testC(MyClass, float, double ) {} 

template <class T, typename... Args>
void applyA(T&& foo, Args... args)
{
    foo(args...);
}

template <class T, typename... Args>
void applyB(T&& foo, Args... args)
{
    MyClass cls;
    foo(cls, args...);
}

int main()
{
    applyA(testA, 5, 0.5f, "abc");
    applyA(testB, 5, 0.5f, 1.5);
    applyB(testC, 0.5f, 1.5);

    return 0;
}

Example with std::apply() c++17

#include <tuple>

...
std::apply(testA, std::make_tuple(5, 0.5f, "abc"));
std::apply(testB, std::make_tuple(5, 0.5f, 1.5));
std::apply(testC, std::make_tuple(MyClass{}, 0.5f, 1.5));

Example with self-made apply () c++11

With help from "unpacking" a tuple to call a matching function pointer SO question's answer.

template<int ...>
struct seq { };

template<int N, int ...S>
struct gens : gens<N-1, N-1, S...> { };

template<int ...S>
struct gens<0, S...> {
  typedef seq<S...> type;
};


template<typename F, typename Tuple, int... S>
void my_apply_impl(F&& func, Tuple&& params, seq<S...> ) {
    func(std::get<S>(std::forward<Tuple>(params)) ...);
}

template<typename F, typename Tuple>
void my_apply(F&& func, Tuple&& params) {
    my_apply_impl(std::forward<F>(func), std::forward<Tuple>(params), typename gens<std::tuple_size<Tuple>::value>::type() );
}

...
my_apply(testA, std::make_tuple(5, 0.5f, "abc"));
my_apply(testB, std::make_tuple(5, 0.5f, 1.5));
my_apply(testC, std::make_tuple(MyClass{}, 0.5f, 1.5));

Demo

like image 180
Mihayl Avatar answered Sep 19 '22 07:09

Mihayl