1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Chapter 5. Frequently Asked Questions (FAQs)</title> 5<link rel="stylesheet" href="boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="index.html" title="Boost.Python"> 8<link rel="up" href="index.html" title="Boost.Python"> 9<link rel="prev" href="support.html" title="Chapter 4. Support Resources"> 10<link rel="next" href="faq/i_m_getting_the_attempt_to_retur.html" title="I'm getting the "attempt to return dangling reference" error. What am I doing wrong?"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="images/boost.png"></td></tr></table> 14<hr> 15<div class="spirit-nav"> 16<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> 17</div> 18<div class="chapter"> 19<div class="titlepage"><div><div><h1 class="title"> 20<a name="faq"></a>Chapter 5. Frequently Asked Questions (FAQs)</h1></div></div></div> 21<div class="toc"><dl class="toc"> 22<dt><span class="section"><a href="faq.html#faq.how_can_i_wrap_a_function_which_">How can I wrap 23 a function which takes a function pointer as an argument?</a></span></dt> 24<dt><span class="section"><a href="faq/i_m_getting_the_attempt_to_retur.html">I'm getting the 25 "attempt to return dangling reference" error. What am I doing wrong?</a></span></dt> 26<dt><span class="section"><a href="faq/is_return_internal_reference_eff.html">Is <code class="computeroutput"><span class="identifier">return_internal_reference</span></code> efficient?</a></span></dt> 27<dt><span class="section"><a href="faq/how_can_i_wrap_functions_which_t.html">How can I wrap 28 functions which take C++ containers as arguments?</a></span></dt> 29<dt><span class="section"><a href="faq/fatal_error_c1204_compiler_limit.html">fatal error C1204:Compiler 30 limit:internal structure overflow</a></span></dt> 31<dt><span class="section"><a href="faq/how_do_i_debug_my_python_extensi.html">How do I debug 32 my Python extensions?</a></span></dt> 33<dt><span class="section"><a href="faq/why_doesn_t_my_operator_work.html">Why doesn't my <code class="computeroutput"><span class="special">*=</span></code> operator work?</a></span></dt> 34<dt><span class="section"><a href="faq/does_boost_python_work_with_mac_.html">Does Boost.Python 35 work with Mac OS X?</a></span></dt> 36<dt><span class="section"><a href="faq/how_can_i_find_the_existing_pyob.html">How can I find 37 the existing PyObject that holds a C++ object?</a></span></dt> 38<dt><span class="section"><a href="faq/how_can_i_wrap_a_function_which0.html">How can I wrap 39 a function which needs to take ownership of a raw pointer?</a></span></dt> 40<dt><span class="section"><a href="faq/compilation_takes_too_much_time_.html">Compilation takes 41 too much time and eats too much memory! What can I do to make it faster?</a></span></dt> 42<dt><span class="section"><a href="faq/how_do_i_create_sub_packages_usi.html">How do I create 43 sub-packages using Boost.Python?</a></span></dt> 44<dt><span class="section"><a href="faq/error_c2064_term_does_not_evalua.html">error C2064: term 45 does not evaluate to a function taking 2 arguments</a></span></dt> 46<dt><span class="section"><a href="faq/how_can_i_automatically_convert_.html">How can I automatically 47 convert my custom string type to and from a Python string?</a></span></dt> 48<dt><span class="section"><a href="faq/why_is_my_automatic_to_python_co.html">Why is my automatic 49 to-python conversion not being found?</a></span></dt> 50<dt><span class="section"><a href="faq/is_boost_python_thread_aware_com.html">Is Boost.Python 51 thread-aware/compatible with multiple interpreters?</a></span></dt> 52</dl></div> 53<div class="section"> 54<div class="titlepage"><div><div><h3 class="title"> 55<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 56 a function which takes a function pointer as an argument?</a> 57</h3></div></div></div> 58<p> 59 If what you're trying to do is something like this: 60 </p> 61<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"><</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">></span> <span class="identifier">funcptr</span><span class="special">;</span> 62 63<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> 64<span class="special">{</span> 65 <span class="identifier">fp</span><span class="special">(</span><span class="string">"hello,world!"</span><span class="special">);</span> 66<span class="special">}</span> 67 68<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">test</span><span class="special">)</span> 69<span class="special">{</span> 70 <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> 71<span class="special">}</span> 72</pre> 73<p> 74 And then: 75 </p> 76<pre class="programlisting"><span class="special">>>></span> <span class="identifier">def</span> <span class="identifier">hello</span><span class="special">(</span><span class="identifier">s</span><span class="special">):</span> 77<span class="special">...</span> <span class="identifier">print</span> <span class="identifier">s</span> 78<span class="special">...</span> 79<span class="special">>>></span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">hello</span><span class="special">)</span> 80<span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span><span class="special">!</span> 81</pre> 82<p> 83 The short answer is: "you can't". This is not a Boost.Python limitation 84 so much as a limitation of C++. The problem is that a Python function is 85 actually data, and the only way of associating data with a C++ function pointer 86 is to store it in a static variable of the function. The problem with that 87 is that you can only associate one piece of data with every C++ function, 88 and we have no way of compiling a new C++ function on-the-fly for every Python 89 function you decide to pass to <code class="computeroutput"><span class="identifier">foo</span></code>. 90 In other words, this could work if the C++ function is always going to invoke 91 the <span class="emphasis"><em>same</em></span> Python function, but you probably don't want 92 that. 93 </p> 94<p> 95 If you have the luxury of changing the C++ code you're wrapping, pass it 96 an <code class="computeroutput"><span class="identifier">object</span></code> instead and call 97 that; the overloaded function call operator will invoke the Python function 98 you pass it behind the <code class="computeroutput"><span class="identifier">object</span></code>. 99 </p> 100</div> 101</div> 102<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 103<td align="left"></td> 104<td align="right"><div class="copyright-footer">Copyright © 2002-2015 David 105 Abrahams, Stefan Seefeld<p> 106 Distributed under the Boost Software License, Version 1.0. (See accompanying 107 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>) 108 </p> 109</div></td> 110</tr></table> 111<hr> 112<div class="spirit-nav"> 113<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> 114</div> 115</body> 116</html> 117