[C++-sig] Ownership question.

David Abrahams dave at boost-consulting.com
Sun Apr 27 19:44:33 CEST 2003


Nicodemus <nicodemus at globalite.com.br> writes:

> Sorry, I didn't explain very well what I meant by "nice". 8)
>
> Suppose that A is a class with a public member "int value" and a
> function foo, which takes the ownership of a A* passed to it. After
> exporting them:
>
>  >>> from test import *
>  >>> a = A()
>  >>> a
> <test.A object at 0x008AA830>
>  >>> a.value
> 0
>  >>> foo(a)
> deleting a
>  >>> a
> <test.A object at 0x008AA830>
>  >>> a.value
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: bad argument type for built-in operation
>  >>> a.value = 10
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: bad argument type for built-in operation
>  >>>
>
> So you see, the object after the call is no longer valid, 

It's valid; it just doesn't contain the C++ "A" object anymore.
nNaturally, you deleted it!

Really "not nice" would be crashing as most naive wrapping systems do.

> which is against the normal Python semantics. The caller must know
> that "foo" is a C++ function and that it takes the ownership of the
> passed object, and the user can no longer use the object. 

It depends on whether you believe exceptions are bad in Python ;-)

> I didn't want to imply that Boost.Python couldn't export this type
> of function: just that the result wouldn't be "pythonic", that's
> all. 8)

It's true; single-ownership semantics are completely un-pythonic.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list