1

是否可以使用boost::fusion::invoke函数调用具有默认参数的函数而不指定这些函数?

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

我正在编写一个用于绑定到脚本语言的包装器,默认参数将大大改善包装器用户的直观感受。恐怕标准并没有涵盖这种情况。

附注:
我知道可以使用仿函数解决它:

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

然而,这不是一个选项,因为我不想强迫用户创建函子只是为了指定默认参数。

4

1 回答 1

1

您可以使用bind更多信息):

boost::fusion::invoke(boost::bind(foo, _1, 1, 2), boost::fusion::vector<int>(0));
于 2010-08-21T12:56:14.513 回答