<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 13 August 2015 at 12:01, Stefan Seefeld <span dir="ltr"><<a href="mailto:stefan@seefeld.name" target="_blank">stefan@seefeld.name</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 13/08/15 06:58 AM, MM wrote:<br>
> On 13 August 2015 at 10:24, MM <<a href="mailto:finjulhich@gmail.com">finjulhich@gmail.com</a><br>
</span><span class="">> <mailto:<a href="mailto:finjulhich@gmail.com">finjulhich@gmail.com</a>>> wrote:<br>
><br>
> I have the following class:<br>
><br>
> class T {<br>
> };<br>
> // T has been exposed to python with class_<br>
><br>
> and free function:<br>
><br>
> void add_T( T* );<br>
><br>
> Ownership of the T* is taken by this C++ function.<br>
><br>
><br>
> If I create an instance of the python version of T, how do I "def"<br>
> the add_T function?<br>
><br>
> def("add_T", add_T)<br>
><br>
> fails to compile.<br>
><br>
><br>
> Apologies. This compiled correctly.<br>
><br>
> This function:<br>
><br>
> const T* get_T( const std::string& name );<br>
><br>
> failed to compile.<br>
><br>
> so the T pointer is owner by a container in the c++ world, it gets<br>
> stored there by add_T,<br>
> then the get_T returns a raw pointer to it. I want to tell python to<br>
> let c++ manage it.<br>
<br>
</span>Sounds like you want to use the "return_internal_reference" call policy<br>
(see<br>
<a href="http://boostorg.github.io/python/doc/html/tutorial/tutorial/functions.html#tutorial.functions.call_policies" rel="noreferrer" target="_blank">http://boostorg.github.io/python/doc/html/tutorial/tutorial/functions.html#tutorial.functions.call_policies</a>).<br>
<span class="HOEnZb"><font color="#888888"><br>
Stefan<br><br>
</font></span></blockquote></div><br></div><div class="gmail_extra">That policy says:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px">"Ties lifetime of one argument to that of result"</span><br></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px">The argument of my function is just the string.... Really its lifetime doesn't matter.... </span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px">In the context of a call from python:</span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px">t = get_T( 'name1' )</span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px">At some point, a std::string temporary must be constructed, holding 'name1'? and then get_T uses it for lookup.</span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px">Once get_T returns the const T*, it doesn't care about it anymore.</span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px">What I want to express is:</span></div><div class="gmail_extra"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></div><div class="gmail_extra"><font color="#000000" face="sans-serif"><span style="font-size:13.3333330154419px;line-height:17.3333320617676px">The t returned by the python function should refer to object T held in c++ memory, and for instance</span></font></div><div class="gmail_extra"><font color="#000000" face="sans-serif"><span style="font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></font></div><div class="gmail_extra"><font color="#000000" face="sans-serif"><span style="font-size:13.3333330154419px;line-height:17.3333320617676px">del t </span></font></div><div class="gmail_extra"><font color="#000000" face="sans-serif"><span style="font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></font></div><div class="gmail_extra"><font color="#000000" face="sans-serif"><span style="font-size:13.3333330154419px;line-height:17.3333320617676px">should not delete the actual T object in c++ memory</span></font></div><div class="gmail_extra"><font color="#000000" face="sans-serif"><span style="font-size:13.3333330154419px;line-height:17.3333320617676px"><br></span></font></div><div class="gmail_extra"><font color="#000000" face="sans-serif"><span style="font-size:13.3333330154419px;line-height:17.3333320617676px">Should I still use </span></font>"return_internal_reference" ?</div></div>