[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

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]':
  instantiated from 'void
boost::python::detail::destroy_referent_impl(void*, T& (*)()) [with T
= const C]'
  instantiated from 'void
boost::python::detail::destroy_referent(void*, T (*)()) [with T =
const C&]'
  instantiated from
[with T = const C&]'
  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&>]'
  instantiated from '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
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.


Patrick L. Hartling
-------------- 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