[C++-sig] Call policy for static property?

Albert Strasheim fullung at gmail.com
Mon Jun 11 16:10:10 CEST 2007


Hello all

I'm trying to add a static property to a class, where this property 
contains an instance of another class.

#include <boost/python/class.hpp>
namespace py = boost::python;
struct Foo{};
struct Bar{};
static Bar& get_bar()
{
    static Bar bar;
    return bar;
}
void export_foo()
{
  py::class_<Bar, boost::noncopyable>("Bar", py::no_init);
  py::class_<Foo, boost::noncopyable>("Foo", py::no_init)
    .add_static_property("theonlybar", &get_bar);
}

so that I can write

mymodule.Foo.theonlybar in Python and get this single instance of the 
Bar class. Unfortunately, this code doesn't compile as-is. I get:

error C2027: use of undefined type 
'boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<T>'	
c:\program files\boost\boost_1_34_0\boost\python\detail\caller.hpp	

which makes sense, since get_bar probably needs a 
return_internal_reference call policy.

I tried to add it with make_function:

make_function(&get_bar, py::return_internal_reference<>())

but it seems make_function is only for wrapping member functions, 
because I get:

IndexError: boost::python::with_custodian_and_ward_postcall: argument 
index out of range

when I try to access the property.

Is there a way to specify a call policy for the function called by a 
static property getter? Alternatively, is there another way to 
accomplish what I'm trying to do?

Thanks for your time.

Regards,

Albert



More information about the Cplusplus-sig mailing list