[C++-sig] wrapping virtual base class with std::auto_ptr

Faheem Mitha faheem at email.unc.edu
Fri May 13 18:36:44 CEST 2005


On Fri, 13 May 2005 08:13:31 -0400, David Abrahams
<dave at boost-consulting.com> wrote:

> Faheem Mitha <faheem at email.unc.edu> writes:
>
>> Hi,
>>
>> Consider the following simple example. This works fine if (for
>> example) I replace std:auto_ptr<int> with int (see commented-put
>> lines). This also works if this is just a regular struct (if f is a
>> normal function returning a std::auto::ptr<int>).
>>
>> The compiler errors I get appear below. The offending line is.
>>
>>     return this->get_override("f")();
>>   
>> I have a dim understanding of how Boost.Python works in general, but
>> really don't understand what magic goes on in wrapping virtual
>> functions etc. I've just copied the syntax below from the tutorial.
>>
>> Thanks in advance for help. Please cc me; I'm not subscribed.
>>
>>                                                           Faheem.
>>
>> ap.cc: In member function `virtual std::auto_ptr<int> BaseWrap::f(int)':
>
> I think this has more to do with auto_ptr than with Boost.Python.  Try
> changing:
>
>   std::auto_ptr<int> f(int val)
>   {
>     return this->get_override("f")();
>  }
>
> into:
>
>   std::auto_ptr<int> f(int val)
>   {
>     std::auto_ptr<int> r( this->get_override("f")() );
>     return r;
>  }

I discovered that using boost::shared_ptr works fine, and since I
couldn't use auto_ptr anyway (since I wanted to put these pointers
inside standard containers), this is what I went with.

I went with std::auto_ptr instead, since I naively thought that
something in the standard library would be more likely to work out of
the box.

My best guess at why std::auto_ptr does not work is that it is a
limitation of how it is designed. Specifically, that the memory in
question cannot be accessed in multiple places simultaneously. Perhaps
Boost.Python is trying to do that, or something similar.

I'll try your modification and see what happens.

                                                             Faheem.




More information about the Cplusplus-sig mailing list