HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

"unpacking" a tuple to call a matching function pointer

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
pointermatchingtuplefunctionunpackingcall

Problem

I'm trying to store in a std::tuple a varying number of values, which will later be used as arguments for a call to a function pointer which matches the stored types.

I've created a simplified example showing the problem I'm struggling to solve:

#include 
#include 

void f(int a, double b, void* c) {
  std::cout 
struct save_it_for_later {
  std::tuple params;
  void (*func)(Args...);

  void delayed_dispatch() {
     // How can I "unpack" params to call func?
     func(std::get(params), std::get(params), std::get(params));
     // But I *really* don't want to write 20 versions of dispatch so I'd rather 
     // write something like:
     func(params...); // Not legal
  }
};

int main() {
  int a=666;
  double b = -1.234;
  void *c = NULL;

  save_it_for_later saved = {
                                 std::tuple(a,b,c), f};
  saved.delayed_dispatch();
}


Normally for problems involving std::tuple or variadic templates I'd write another template like template to recursively evaluate all of the types one by one, but I can't see a way of doing that for dispatching a function call.

The real motivation for this is somewhat more complex and it's mostly just a learning exercise anyway. You can assume that I'm handed the tuple by contract from another interface, so can't be changed but that the desire to unpack it into a function call is mine. This rules out using std::bind as a cheap way to sidestep the underlying problem.

What's a clean way of dispatching the call using the std::tuple, or an alternative better way of achieving the same net result of storing/forwarding some values and a function pointer until an arbitrary future point?

Solution

The C++17 solution is simply to use std::apply:

auto f = [](int a, double b, std::string c) { std::cout<<a<<" "<<b<<" "<<c<< std::endl; };
auto params = std::make_tuple(1,2.0,"Hello");
std::apply(f, params);


Just felt that should be stated once in an answer in this thread (after it already appeared in one of the comments).

The basic C++14 solution is still missing in this thread. EDIT: No, it's actually there in the answer of Walter.

This function is given:

void f(int a, double b, void* c)
{
      std::cout << a << ":" << b << ":" << c << std::endl;
}


Call it with the following snippet:

template
auto call(Function f, Tuple t, std::index_sequence)
{
     return f(std::get(t) ...);
}

template
auto call(Function f, Tuple t)
{
    static constexpr auto size = std::tuple_size::value;
    return call(f, t, std::make_index_sequence{});
}


Example:

int main()
{
    std::tuple t;
    //or std::array t;
    //or std::pair t;
    call(f, t);    
}


DEMO

Code Snippets

auto f = [](int a, double b, std::string c) { std::cout<<a<<" "<<b<<" "<<c<< std::endl; };
auto params = std::make_tuple(1,2.0,"Hello");
std::apply(f, params);
void f(int a, double b, void* c)
{
      std::cout << a << ":" << b << ":" << c << std::endl;
}
template<typename Function, typename Tuple, size_t ... I>
auto call(Function f, Tuple t, std::index_sequence<I ...>)
{
     return f(std::get<I>(t) ...);
}

template<typename Function, typename Tuple>
auto call(Function f, Tuple t)
{
    static constexpr auto size = std::tuple_size<Tuple>::value;
    return call(f, t, std::make_index_sequence<size>{});
}
int main()
{
    std::tuple<int, double, int*> t;
    //or std::array<int, 3> t;
    //or std::pair<int, double> t;
    call(f, t);    
}

Context

Stack Overflow Q#7858817, score: 129

Revisions (0)

No revisions yet.