Humam Helfawi Humam Helfawi - 1 year ago 85
C++ Question

Get set of arguments as result of a function

I do not know the name of this pattern or even if it is exist. I need a function that return a set of arguments that would be used in another function. For example, I have this function:

void foo(int a, float b, some_class c);

I want to write a function like this:

//Theoretical code:
arguments get_arguments(){
return arguments(0,0.0f, some_class());

Then call
like this:


Is this possible? if yes,How?

Answer Source

get_arguments can be implemented with std::make_tuple:

auto get_arguments(){
    return std::make_tuple(0,0.0f, some_class());

This will return a std::tuple<int,float,some_class>.

You can call foo with the arguments using std::experimental::apply from C++17:

std::experimental::apply(foo, get_arguments());

Live Demo

There is an implementation of std::experimental::apply here if you need one.

To clean up the call, you could add a forwarding function:

template <typename Tuple>
void foo(Tuple&& t) {
    return std::experimental::apply(

Then you can use the syntax you desire:


Live Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download