[C++-sig] Problems passing objects to function by const reference

Patrick Hartling patrick.hartling at gmail.com
Tue Oct 16 17:00:58 CEST 2007


In our uses of Boost.Python, we have run into a case where very simple code
fails to compile. The case that we have is that a class type has no public
destructor and instantiations of that type are passed by const reference into
a function. The Boost.Python code for exposing the function that accepts the
objects fail to compile because an attempt is made to call the non-public
destructor.

The attached code demonstrates what we are trying to do. The compile error
from GCC 4.1 when compiling against Boost 1.34.1 is as follows:

destroy_test.h: In static member function 'static void
boost::python::detail::value_destroyer<false>::execute(const volatile
T*) [with T = C]':
.../boost-1.34.1/include/boost-1_34_1/boost/python/detail/destroy.hpp:90:
  instantiated from 'void
boost::python::detail::destroy_referent_impl(void*, T& (*)()) [with T
= const C]'
.../boost-1.34.1/include/boost-1_34_1/boost/python/detail/destroy.hpp:101:
  instantiated from 'void
boost::python::detail::destroy_referent(void*, T (*)()) [with T =
const C&]'
.../boost-1.34.1/include/boost-1_34_1/boost/python/converter/rvalue_from_python_data.hpp:135:
  instantiated from
'boost::python::converter::rvalue_from_python_data<T>::~rvalue_from_python_data()
[with T = const C&]'
.../boost-1.34.1/include/boost-1_34_1/boost/python/converter/arg_from_python.hpp:108:
  instantiated from 'PyObject*
boost::python::detail::caller_arity<1u>::impl<F, Policies,
Sig>::operator()(PyObject*, PyObject*) [with F = void (*)(const C&),
Policies = boost::python::default_call_policies, Sig =
boost::mpl::vector2<void, const C&>]'
.../boost-1.34.1/include/boost-1_34_1/boost/python/object/py_function.hpp:38:
  instantiated from 'PyObject*
boost::python::objects::caller_py_function_impl<Caller>::operator()(PyObject*,
PyObject*) [with Caller = boost::python::detail::caller<void (*)(const
C&), boost::python::default_call_policies, boost::mpl::vector2<void,
const C&> >]'
generated.cpp:21:   instantiated from here
destroy_test.h:4: error: 'C::~C()' is protected
.../boost-1.34.1/include/boost-1_34_1/boost/python/detail/destroy.hpp:33:
error: within this context

I have tried to understand what Boost.Python is doing internally, and it
seems like a copy of the object is being made that is later cleaned up by
calling the destructor explicitly. I could definitely be wrong about that
assessment, though. Ultimately, what I don't understand is why anything needs
to be destroyed in this case.

 -Patirck


-- 
Patrick L. Hartling
http://www.137.org/patrick/
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: destroy_test.h
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20071016/afb2c1f7/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: generated.cpp
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20071016/afb2c1f7/attachment-0001.txt>


More information about the Cplusplus-sig mailing list