views:

28

answers:

1

Is it possible to use boost::fusion::invoke function to call a function that has default arguments without specifying those?

Example:

void foo(int x, int y = 1, int z = 2)
{
  std::cout << "The sum is: " << (x + y + z) << std::endl;
}

...

// This should call foo(0). It doesn't work because the type of foo is void (*) (int, int, int).
boost::fusion::invoke(foo, boost::fusion::vector<int>(0));

// Works
boost::fusion::invoke(foo, boost::fusion::vector<int, int, int>(0, 1, 2));

I am writing a wrapper for bindings to a scripting language and default arguments would greatly improve the intuitive feel for the users of the wrapper. I am afraid though that the standard has not covered this case.

A side note:
I know one could work around it using functors:

struct foo  {
  void operator() (int x, int y = 1, int z = 2)  { /* ... */ }
};

// Works because the functor adds an indirection
boost::fusion::invoke(foo(), boost::fusion::vector<int>(0));

This is however not an option as I don't want to force the users to create functors just to specify default arguments.

+1  A: 

You can use bind (more info):

boost::fusion::invoke(boost::bind(foo, _1, 1, 2), boost::fusion::vector<int>(0));
PC2st
Thanks, that seems like a reasonable solution. I was more interested in specifying the default arguments directly in the function declaration but it seems that it's simply impossible to combine those with invoke().
dark_charlie