Boost.Python 没有默认构造函数和唯一指针时库构建错误
Boost.Python library build errors when no default constructor and unique pointer
我正在尝试使用 Boost.Python 包装我的 C++ 代码,遇到了一些错误,需要一些帮助来解决这些错误。但首先是我拥有的资产。
Product.h
namespace Products
{
class Product
{
public:
virtual ~Product() {};
virtual int generateProduct(int verbose = 0) = 0;
virtual int getQueueNum() = 0;
virtual std::vector<std::vector<std::string> > getQueue(const int &index_int) = 0;
};
}
ProductBuilder.h
namespace Products
{
class ProductBuilder
{
public:
ProductBuilder(const std::map<std::string,std::string> input_map, const std::deque<std::string> &dates_deque);
int build(int verbose = 0);
int numQueue() {return m_product->getQueueNum();};
std::vector<std::vector<std::string> > getQueueByIndex(const int &index_int) {return m_product->getQueue(index_int);};
private:
std::unique_ptr<Products::Product> m_product;
};
}
全-wrapper.cpp
BOOST_PYTHON_MODULE(alucard)
{
class_<Products::ProductBuilder>("ProductBuilder", init<std::map<std::string,std::string>,std::deque<std::string>>())
.def("build", &Products::ProductBuilder::build)
.def("numQueue", &Products::ProductBuilder::numQueue)
.def("getQueueByIndex", &Products::ProductBuilder::getQueueByIndex)
;
}
我通过cmake构建库时出现的错误如下:
/usr/include/boost/python/object/value_holder.hpp:133:13: error: use of deleted function ‘Products::ProductBuilder::ProductBuilder(const Products::ProductBuilder&)’
BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
^
In file included from /home/jlahowetz2/development/cpp-python-wrapper/full-wrapper.cpp:11:0:
/data/alucard/include/ProductBuilder.h:25:9: note: ‘Products::ProductBuilder::ProductBuilder(const Products::ProductBuilder&)’ is implicitly deleted because the default definition would be ill-formed:
class ProductBuilder
^~~~~~~~~~~~~~
/data/alucard/include/ProductBuilder.h:25:9: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Products::Product; _Dp = std::default_delete<Products::Product>]’
In file included from /usr/include/c++/7/memory:80:0,
from /usr/include/boost/function/function_base.hpp:16,
from /usr/include/boost/function/detail/prologue.hpp:17,
from /usr/include/boost/function/function_template.hpp:13,
from /usr/include/boost/function/detail/maybe_include.hpp:13,
from /usr/include/boost/function/function0.hpp:11,
from /usr/include/boost/python/errors.hpp:13,
from /usr/include/boost/python/handle.hpp:11,
from /usr/include/boost/python/args_fwd.hpp:10,
from /usr/include/boost/python/args.hpp:10,
from /usr/include/boost/python.hpp:11,
from /home/jlahowetz2/development/cpp-python-wrapper/full-wrapper.cpp:1:
/usr/include/c++/7/bits/unique_ptr.h:383:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^~~~~~~~~~
显然 Python 代码复制了您的 ProductBuilder 对象。但是,ProductBuilder 包含一个无法复制(只能移动)的 unique_ptr。解决此问题的一种简单方法是将 unique_ptr 替换为可复制的 shared_ptr。
我正在尝试使用 Boost.Python 包装我的 C++ 代码,遇到了一些错误,需要一些帮助来解决这些错误。但首先是我拥有的资产。
Product.h
namespace Products
{
class Product
{
public:
virtual ~Product() {};
virtual int generateProduct(int verbose = 0) = 0;
virtual int getQueueNum() = 0;
virtual std::vector<std::vector<std::string> > getQueue(const int &index_int) = 0;
};
}
ProductBuilder.h
namespace Products
{
class ProductBuilder
{
public:
ProductBuilder(const std::map<std::string,std::string> input_map, const std::deque<std::string> &dates_deque);
int build(int verbose = 0);
int numQueue() {return m_product->getQueueNum();};
std::vector<std::vector<std::string> > getQueueByIndex(const int &index_int) {return m_product->getQueue(index_int);};
private:
std::unique_ptr<Products::Product> m_product;
};
}
全-wrapper.cpp
BOOST_PYTHON_MODULE(alucard)
{
class_<Products::ProductBuilder>("ProductBuilder", init<std::map<std::string,std::string>,std::deque<std::string>>())
.def("build", &Products::ProductBuilder::build)
.def("numQueue", &Products::ProductBuilder::numQueue)
.def("getQueueByIndex", &Products::ProductBuilder::getQueueByIndex)
;
}
我通过cmake构建库时出现的错误如下:
/usr/include/boost/python/object/value_holder.hpp:133:13: error: use of deleted function ‘Products::ProductBuilder::ProductBuilder(const Products::ProductBuilder&)’
BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_UNFORWARD_LOCAL, nil)
^
In file included from /home/jlahowetz2/development/cpp-python-wrapper/full-wrapper.cpp:11:0:
/data/alucard/include/ProductBuilder.h:25:9: note: ‘Products::ProductBuilder::ProductBuilder(const Products::ProductBuilder&)’ is implicitly deleted because the default definition would be ill-formed:
class ProductBuilder
^~~~~~~~~~~~~~
/data/alucard/include/ProductBuilder.h:25:9: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Products::Product; _Dp = std::default_delete<Products::Product>]’
In file included from /usr/include/c++/7/memory:80:0,
from /usr/include/boost/function/function_base.hpp:16,
from /usr/include/boost/function/detail/prologue.hpp:17,
from /usr/include/boost/function/function_template.hpp:13,
from /usr/include/boost/function/detail/maybe_include.hpp:13,
from /usr/include/boost/function/function0.hpp:11,
from /usr/include/boost/python/errors.hpp:13,
from /usr/include/boost/python/handle.hpp:11,
from /usr/include/boost/python/args_fwd.hpp:10,
from /usr/include/boost/python/args.hpp:10,
from /usr/include/boost/python.hpp:11,
from /home/jlahowetz2/development/cpp-python-wrapper/full-wrapper.cpp:1:
/usr/include/c++/7/bits/unique_ptr.h:383:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^~~~~~~~~~
显然 Python 代码复制了您的 ProductBuilder 对象。但是,ProductBuilder 包含一个无法复制(只能移动)的 unique_ptr。解决此问题的一种简单方法是将 unique_ptr 替换为可复制的 shared_ptr。