为什么boost :: factory在具有by-value构造函数参数的类中失败?

时间:2017-05-14 10:25:04

标签: c++ boost

我在使用boost :: factory时遇到问题,因为这个类有一个带有by-value参数的构造函数。如果构造函数具有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* >();

如果我更改此代码,以便D构造函数需要int而不是int&amp;:

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

我收到编译错误:

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
. . .

为什么在D:s构造函数具有非引用参数时无法创建D工厂?

我正在使用clang版本3.9.1和BOOST_LIB_VERSION&#34; 1_64&#34;。

0 个答案:

没有答案