2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00
Files
python/doc/html/faq.html
2020-12-25 16:51:28 +00:00

85 lines
5.9 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chapter&#160;5.&#160;Frequently Asked Questions (FAQs)</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="Boost.Python">
<link rel="up" href="index.html" title="Boost.Python">
<link rel="prev" href="support.html" title="Chapter&#160;4.&#160;Support Resources">
<link rel="next" href="faq/i_m_getting_the_attempt_to_retur.html" title="I'm getting the &quot;attempt to return dangling reference&quot; error. What am I doing wrong?">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="images/bpl.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="support.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="faq/i_m_getting_the_attempt_to_retur.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="faq"></a>Chapter&#160;5.&#160;Frequently Asked Questions (FAQs)</h1></div></div></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="faq.how_can_i_wrap_a_function_which_"></a><a class="link" href="faq.html#faq.how_can_i_wrap_a_function_which_" title="How can I wrap a function which takes a function pointer as an argument?">How can I wrap
a function which takes a function pointer as an argument?</a>
</h3></div></div></div>
<p>
If what you're trying to do is something like this:
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">void</span> <span class="special">(</span><span class="identifier">string</span> <span class="identifier">s</span><span class="special">)</span> <span class="special">&gt;</span> <span class="identifier">funcptr</span><span class="special">;</span>
<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">funcptr</span> <span class="identifier">fp</span><span class="special">)</span>
<span class="special">{</span>
<span class="identifier">fp</span><span class="special">(</span><span class="string">"hello,world!"</span><span class="special">);</span>
<span class="special">}</span>
<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">test</span><span class="special">)</span>
<span class="special">{</span>
<span class="identifier">def</span><span class="special">(</span><span class="string">"foo"</span><span class="special">,</span><span class="identifier">foo</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
And then:
</p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">def</span> <span class="identifier">hello</span><span class="special">(</span><span class="identifier">s</span><span class="special">):</span>
<span class="special">...</span> <span class="identifier">print</span> <span class="identifier">s</span>
<span class="special">...</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span>
<span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span><span class="special">!</span>
</pre>
<p>
The short answer is: "you can't". This is not a Boost.Python limitation
so much as a limitation of C++. The problem is that a Python function is
actually data, and the only way of associating data with a C++ function pointer
is to store it in a static variable of the function. The problem with that
is that you can only associate one piece of data with every C++ function,
and we have no way of compiling a new C++ function on-the-fly for every Python
function you decide to pass to <code class="computeroutput"><span class="identifier">foo</span></code>.
In other words, this could work if the C++ function is always going to invoke
the <span class="emphasis"><em>same</em></span> Python function, but you probably don't want
that.
</p>
<p>
If you have the luxury of changing the C++ code you're wrapping, pass it
an <code class="computeroutput"><span class="identifier">object</span></code> instead and call
that; the overloaded function call operator will invoke the Python function
you pass it behind the <code class="computeroutput"><span class="identifier">object</span></code>.
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2015 David
Abrahams, Stefan Seefeld<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="support.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="faq/i_m_getting_the_attempt_to_retur.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>