2

I want to call function by function pointer that have unknown arguments. store input arguments and function and run it later. some thing like call_user_func_array in php

for example:

// example function definition
void foo(int arg1, const char *arg2,const char *arg3){
    //some other code
}
void bar(int arg1, int arg2){
    //some other code
}

// function definition
void store_function_call(int param,void *function,... args){
    // store param , function, and other arguments
    // in php save to global variable for use later
}
void call_later(){
    // execute stored param, function, arguments
    // in PHP use call_user_func_array
}
// use:
int main(){
    store_function_call(10,bar,4,5);
    // store_function_call(5,foo,5,"yyy","sss");

    call_later();
}
seyed
  • 1,555
  • 1
  • 17
  • 24

1 Answers1

2

You can do what you're trying to do in C++11 with a little bit of template metaprogramming:

#include <tuple>
#include <iostream>

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; };

double foo(int x, float y, double z) {
  return x + y + z;
}

template <typename R, typename ...Args>
struct save_it_for_later {
  std::tuple<Args...> params;
  R (*func)(Args...);

  R call_later() {
    return callFunc(typename gens<sizeof...(Args)>::type());
  }

  template<int ...S>
  R callFunc(seq<S...>) {
    return func(std::get<S>(params) ...);
  }
};

template <typename R, typename ...Args>
save_it_for_later<R, Args...> store_function_call(R (*func)(Args...), Args&& ...args) {
  return save_it_for_later<R, Args...>{std::tuple<Args...>(std::forward<Args>(args)...), func};
}

int main() {
  auto saved = store_function_call(foo,1,1.f,1.);
  std::cout << saved.call_later() << "\n";
}

This answer is modified to match your scenario from this answer to a similar question I asked. I've added return type deduction and a helper to deduce the types as your store_function_call.

(I would have used std::forward_as_tuple instead of the clunky tuple constructor+forward construct, but the compiler I tested on doesn't have that)

Community
  • 1
  • 1
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • 1
    @seyed You will need to enable C++11 on your compiler. For gcc `-std=c++0x` is sufficient. – Flexo Feb 10 '13 at 10:17
  • how i can store array (or vector) of return values of `store_function_call`? – seyed Feb 10 '13 at 20:05
  • 1
    @seyed - you need some type erasure. `std::function` in C++11 gives you exactly that in a handy form, e.g. `std::vector> stored_functions; stored_functions.push_back(store_function_call(foo, 1));` – Flexo Feb 10 '13 at 20:08