[C++-sig] return_internal_reference<>()

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Fri May 16 18:34:02 CEST 2003

--- Cyril Bonnard <cyril.bonnard at cirad.fr> wrote:
> hi, iam newbie in using boost.python, and i don't understand why the 
> following code doesn't work:
> hello.h:
> ....
>          int& geti()     { return i; }
> ...
> hello.cpp:
> ....
>        .def("geti", &World::geti, return_internal_reference<>())
> ...
> i noticed that this works if there is a class (string, inner....) 
> instead of int or uchar....
> so waht i done wrong and what shoud be be the right policy to return 
> int& or unsigned char&....

I think it doesn't work because integers and strings are immutable in Python.
If you need to modify the integer from Python you will need a setter, along the
lines of

  void seti(your_type& o, int new_i)
    o.geti() = new_i;


  .def("seti", seti)

Note that this adds the non-member C++ seti() function as a member function of
the Python type. One of the many cool features of Boost.Python.

I am not sure what the right return value policy is for your geti() function,
but it has to be one which returns a copy.


Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.

More information about the Cplusplus-sig mailing list