faq.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Chapter&#160;5.&#160;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&#160;4.&#160;Support Resources">
  10. <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?">
  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&#160;5.&#160;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">&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>
  62. <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>
  63. <span class="special">{</span>
  64. <span class="identifier">fp</span><span class="special">(</span><span class="string">"hello,world!"</span><span class="special">);</span>
  65. <span class="special">}</span>
  66. <span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">test</span><span class="special">)</span>
  67. <span class="special">{</span>
  68. <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>
  69. <span class="special">}</span>
  70. </pre>
  71. <p>
  72. And then:
  73. </p>
  74. <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>
  75. <span class="special">...</span> <span class="identifier">print</span> <span class="identifier">s</span>
  76. <span class="special">...</span>
  77. <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>
  78. <span class="identifier">hello</span><span class="special">,</span> <span class="identifier">world</span><span class="special">!</span>
  79. </pre>
  80. <p>
  81. The short answer is: "you can't". This is not a Boost.Python limitation
  82. so much as a limitation of C++. The problem is that a Python function is
  83. actually data, and the only way of associating data with a C++ function pointer
  84. is to store it in a static variable of the function. The problem with that
  85. is that you can only associate one piece of data with every C++ function,
  86. and we have no way of compiling a new C++ function on-the-fly for every Python
  87. function you decide to pass to <code class="computeroutput"><span class="identifier">foo</span></code>.
  88. In other words, this could work if the C++ function is always going to invoke
  89. the <span class="emphasis"><em>same</em></span> Python function, but you probably don't want
  90. that.
  91. </p>
  92. <p>
  93. If you have the luxury of changing the C++ code you're wrapping, pass it
  94. an <code class="computeroutput"><span class="identifier">object</span></code> instead and call
  95. that; the overloaded function call operator will invoke the Python function
  96. you pass it behind the <code class="computeroutput"><span class="identifier">object</span></code>.
  97. </p>
  98. </div>
  99. </div>
  100. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  101. <td align="left"></td>
  102. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2015 David
  103. Abrahams, Stefan Seefeld<p>
  104. Distributed under the Boost Software License, Version 1.0. (See accompanying
  105. 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>)
  106. </p>
  107. </div></td>
  108. </tr></table>
  109. <hr>
  110. <div class="spirit-nav">
  111. <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>
  112. </div>
  113. </body>
  114. </html>