使用带有boost python的unique_ptr - boost :: shared_ptr有效,但unique_ptr不行

时间:2014-11-29 21:26:46

标签: python c++ boost boost-python unique-ptr

这可能与

相同

Boost Python No to_python for std::unique_ptr

然而,我还没有看到回应,而且不清楚这是否是一个' boost-python'问题或由于我特别使用' std :: unique_ptr' 我试图找出为什么更改从boost :: shared_ptr派生的类在使用" std :: unique_ptr []"

时会破坏编译

下面是我的boost python模块,其中包含2个可能的' smart_array'类 原文基于boost :: shared_array,工作正常(如下所示)

当我使用基于unique_ptr(简单替换)的一个时,我会遇到令人困惑的编译错误。

// Boost Includes ==============================================================
#include <boost/python.hpp>
#include <boost/cstdint.hpp>

#define NO_BOOST
#ifdef NO_BOOST
#include "smart_array.h"
#else
#include "boost_smart_array.h"
#endif
template <class Numeric> class cic {
public:
        smart_array<Numeric> nacc; //! Accumulators
        cic(int s) :  nacc(3) {
                for (int i=0;i<3;i++) nacc[i] = (Numeric)0;
        }
        Numeric interpolate() {         return(nacc[2]);        }
};

// Using =======================================================================
using namespace boost::python;
BOOST_PYTHON_MODULE(cic_double)
{
        class_< cic<double> >("cic_double", init< int >())
                .def("interpolate", &cic<double>::interpolate);
}

- 智能阵列类(原始 - 提升)

#include "boost/shared_array.hpp"
    template<class T> class smart_array : public boost::shared_array<T> {
    public:
            //! Default constructor
            smart_array() {} 
            //! Create an smart_array of size n
            smart_array(long n) : boost::shared_array<T>(new T[n]) {
                    elements = n;
            }
            void resize(long n) { 
                    elements = n;
                    boost::shared_array<T>::reset(new T[n]); 
            }
            long len() const { return(elements); }
    private:
            long elements;
    };

新功能 - 基于unique_ptr

template<class T> class smart_array : public std::unique_ptr<T []> {
 public:
  //! Default constructor
  smart_array() {} 
  //! Create an smart_array of size n
  smart_array(long n) : std::unique_ptr<T []>(new T[n]) {
                elements = n;
  }
  void resize(long n) { 
                elements = n;
                std::unique_ptr<T []>::reset(new T[n]); 
  }
  long len() const { return(elements); }
 private:
  long elements;
};

编译错误

[100%] Building CXX object CMakeFiles/cic_double.dir/py_cic_double.cpp.o
In file included from /Users/user/GitHubStuff/boost_cic_test/py_cic_double.cpp:2:
In file included from /usr/local/include/boost/python.hpp:18:
In file included from /usr/local/include/boost/python/class.hpp:23:
In file included from /usr/local/include/boost/python/object/class_metadata.hpp:11:
In file included from /usr/local/include/boost/python/object/value_holder.hpp:50:
In file included from /usr/local/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52:
/usr/local/include/boost/python/object/value_holder.hpp:135:11: error: no matching constructor for initialization of 'cic<double>'
        : m_held(
          ^
/usr/local/include/boost/python/object/make_instance.hpp:71:30: note: in instantiation of function template specialization 'boost::python::objects::value_holder<cic<double> >::value_holder<boost::reference_wrapper<const cic<double> > >' requested here
        return new (storage) Holder(instance, x);
                             ^
/usr/local/include/boost/python/object/make_instance.hpp:45:22: note: in instantiation of member function 'boost::python::objects::make_instance<cic<double>, boost::python::objects::value_holder<cic<double> > >::construct' requested here
            Derived::construct(&instance->storage, (PyObject*)instance, x)->install(raw_result);
                     ^
/usr/local/include/boost/python/object/class_wrapper.hpp:29:30: note: in instantiation of function template specialization 'boost::python::objects::make_instance_impl<cic<double>, boost::python::objects::value_holder<cic<double> >, boost::python::objects::make_instance<cic<double>, boost::python::objects::value_holder<cic<double> > > >::execute<const boost::reference_wrapper<const cic<double> > >' requested here
        return MakeInstance::execute(boost::ref(x));
                             ^
/usr/local/include/boost/python/converter/as_to_python_function.hpp:27:72: note: in instantiation of member function 'boost::python::objects::class_cref_wrapper<cic<double>, boost::python::objects::make_instance<cic<double>, boost::python::objects::value_holder<cic<double> > > >::convert' requested here
        convert_function_must_take_value_or_const_reference(&ToPython::convert, 1L);
                                                                       ^
/usr/local/include/boost/python/to_python_converter.hpp:88:22: note: in instantiation of member function 'boost::python::converter::as_to_python_function<cic<double>, boost::python::objects::class_cref_wrapper<cic<double>, boost::python::objects::make_instance<cic<double>, boost::python::objects::value_holder<cic<double> > > > >::convert' requested here
        &normalized::convert
                     ^
/usr/local/include/boost/python/object/class_wrapper.hpp:24:8: note: (skipping 2 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
struct class_cref_wrapper
       ^
/usr/local/include/boost/python/object/class_metadata.hpp:219:25: note: in instantiation of function template specialization 'boost::python::objects::class_metadata<cic<double>, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::register_aux2<cic<double>, boost::integral_constant<bool, false> >' requested here
        class_metadata::register_aux2((T*)0, use_callback());
                        ^
/usr/local/include/boost/python/object/class_metadata.hpp:205:25: note: in instantiation of member function 'boost::python::objects::class_metadata<cic<double>, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::register_aux' requested here
        class_metadata::register_aux((T*)0);
                        ^
/usr/local/include/boost/python/class.hpp:497:19: note: in instantiation of member function 'boost::python::objects::class_metadata<cic<double>, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::register_' requested here
        metadata::register_(); // set up runtime metadata/conversions
                  ^
/usr/local/include/boost/python/class.hpp:209:15: note: in instantiation of function template specialization 'boost::python::class_<cic<double>, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::initialize<boost::python::init_base<boost::python::init<int, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_> > >' requested here
        this->initialize(i);
              ^
/Users/user/GitHubStuff/boost_cic_test/py_cic_double.cpp:24:2: note: in instantiation of function template specialization 'boost::python::class_<cic<double>, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::class_<boost::python::init<int, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_, mpl_::void_> >' requested here
        class_< cic<double> >("cic_double", init< int >())
        ^
/Users/user/GitHubStuff/boost_cic_test/py_cic_double.cpp:11:32: note: candidate constructor (the implicit copy constructor) not viable: 1st argument ('typename reference_wrapper<const cic<double> >::type' (aka 'const cic<double>')) would lose const qualifier
template <class Numeric> class cic {
                               ^
/Users/user/GitHubStuff/boost_cic_test/py_cic_double.cpp:14:2: note: candidate constructor not viable: no known conversion from 'typename reference_wrapper<const cic<double> >::type' (aka 'const cic<double>') to 'int' for 1st argument
        cic(int s) :  nacc(3) {
        ^
1 error generated.
make[3]: *** [CMakeFiles/cic_double.dir/py_cic_double.cpp.o] Error 1
make[2]: *** [CMakeFiles/cic_double.dir/all] Error 2
make[1]: *** [all] Error 2
make: *** [all] Error 2

1 个答案:

答案 0 :(得分:4)

不幸的是,目前,不可能使用boost :: python公开std::unique_ptr,因为它需要移动语义,而boost :: python还不支持这些(更多细节{{3 }})。此外,你不应该从智能指针派生,因为它是here并且它们没有虚拟析构函数(除了许多其他原因)

说,你有很多其他的选择,其中一些是:

  • 使用std::auto_ptr作为数据成员,boost::python得到了很好的支持(假设您的课程不可复制)。但是,auto_ptr无法保存数组(它不会调用delete()的正确变体)。
  • 使用boost::shared_ptr作为数据成员,并隐藏其在API中的使用。但是,请注意boost::shared_ptr not a good practice,原因与上述相同。
  • 使用cannot not hold array data打包std::vector<boost::shared_ptr<>>,它将与boost::shared_ptr开箱即用。
  • ......还有很多其他人。

然而,还有很多其他方法,我个人建议重新设计你的代码,以使用boost :: python示例中一些更普遍接受的编码和python-wrapping实践。