Boost.Fusion功能:使用默认参数调用函数

时间:2010-08-21 12:35:09

标签: c++ function metaprogramming function-pointers boost-fusion

是否可以使用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));

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

1 个答案:

答案 0 :(得分:1)

您可以使用bindmore info):

boost::fusion::invoke(boost::bind(foo, _1, 1, 2), boost::fusion::vector<int>(0));
相关问题