[C++-sig] boost.python internal references on basic types

Roman Yakovenko roman.yakovenko at gmail.com
Tue Jun 17 15:20:58 CEST 2008


On Tue, Jun 17, 2008 at 3:51 PM, Renaud Lepere <renaud.lepere at alma.fr> wrote:
>
> I would like to wrap member functions returning internal references on
> int / double. I found an example
> similar  to my problem but not exactly the same on
> http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/return_internal_
> reference.html
>
> Here is my simplified example
>
> class Foo
> {
> public:
>    void set_x(int & x) { m_x = x;}
>    const int & get_x() { return m_x; }
>    int m_x;
> };
> BOOST_PYTHON_MODULE(hello)
> {
>   class_<Foo>("Foo")
>       .def("set_x", &Foo::set_x)
>       .def("get_x", &Foo::get_x,
>             return_internal_reference< >() )
>       ;
> }
>
> this leads me to the given error
> ..\boost/python/object/make_instance.hpp(24) : error C2027: utilisation
> du type non defini 'boost::STATIC_ASSERTION_FAILURE<x>'
> this corresponds to BOOST_STATIC_ASSERT(is_class<T>::value);
>
> If i replace "int" with a proxy class containing the int evrything is ok
> ; but i would prefer
> not to change the c++ code.
>
> Can someone have an idea if what i try to do is possible ?

It is not possible. Integer is immutable type in Python. Even ctypes
modules returns new instance of class int. You can wrap it with a
proxy class, but It will not be natural for Python users.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list