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();
}
1
  • You probably want to use Boost::bind. Commented Feb 10, 2013 at 9:42

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

@seyed You will need to enable C++11 on your compiler. For gcc -std=c++0x is sufficient.
how i can store array (or vector) of return values of store_function_call?
@seyed - you need some type erasure. std::function in C++11 gives you exactly that in a handy form, e.g. std::vector<std::function<void ()>> stored_functions; stored_functions.push_back(store_function_call(foo, 1));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.