C++ 为什么boost::factory对于具有by value构造函数参数的类失败?

C++ 为什么boost::factory对于具有by value构造函数参数的类失败?,c++,boost,C++,Boost,对于一个具有按值参数的构造函数的类,我在使用boost::factory时遇到问题。如果构造函数具有by-reference参数,则该代码将起作用 以下是一些有效的代码: struct B { virtual ~B() {} virtual void work() = 0; }; struct D : public B { D(int& a) : val(a) { } void work() { std::cout << val << std::

对于一个具有按值参数的构造函数的类,我在使用boost::factory时遇到问题。如果构造函数具有by-reference参数,则该代码将起作用

以下是一些有效的代码:

struct B {
  virtual ~B() {}
  virtual void work() = 0;
};

struct D : public B {
  D(int& a) : val(a) { }
  void work() { std::cout << val << std::endl; } 
  int val;
};

boost::function< B* (int&) > creator = boost::factory< D* >();
结构B{ 虚拟~B(){} 虚空功()=0; }; 结构D:公共B{ D(int&a):val(a){} void work(){std::cout(); 如果我更改此代码,使D构造函数需要int而不是int&:

struct D : public B {
  D(int a) : val(a) { }
  void work() { std::cout << val << std::endl; } 
  int val;
};

boost::function< B* (int) > creator = boost::factory< D* >();
结构D:公共B{ D(inta):val(a){} void work(){std::cout(); 我得到一个编译错误:

boost/function/function_template.hpp:138:18: error: no matching function for call to object of type 'boost::factory<D *, void,
  boost::factory_alloc_propagation::factory_alloc_for_pointee_and_deleter>'
      return (*f)(BOOST_FUNCTION_ARGS);
. . .
boost/functional/factory.hpp:155:24: note: candidate function [with T0 = int] not viable: expects an l-value for 1st argument
inline result_type operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,& a)) const
. . .
boost/function/function_template.hpp:138:18:错误:对“boost::factory”类型的对象的调用没有匹配的函数
返回(*f)(增压功能参数);
. . .
boost/functional/factory.hpp:155:24:注意:候选函数[T0=int]不可行:第一个参数需要一个l值
内联结果类型运算符()
. . .
当D:s构造函数有一个非引用参数时,为什么不可能创建一个D工厂


我使用CLAN版本3.9-1和BooStLyBBY版本“116”。< / P>为什么不使用C++ lambda,如果需要传递它,可以将其存储在<代码>:函数< /代码>。这是个诀窍,谢谢!