to_from_python_type_conversion.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Chapter&#160;5.&#160;To/From Python Type Conversion</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 Reference Manual">
  8. <link rel="up" href="index.html" title="Boost.Python Reference Manual">
  9. <link rel="prev" href="function_invocation_and_creation/models_of_resultconvertergenerat.html" title="Models of ResultConverterGenerator">
  10. <link rel="next" href="to_from_python_type_conversion/boost_python_implicit_hpp.html" title="boost/python/implicit.hpp">
  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="function_invocation_and_creation/models_of_resultconvertergenerat.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="to_from_python_type_conversion/boost_python_implicit_hpp.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="to_from_python_type_conversion"></a>Chapter&#160;5.&#160;To/From Python Type Conversion</h1></div></div></div>
  21. <div class="toc">
  22. <p><b>Table of Contents</b></p>
  23. <dl class="toc">
  24. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp">boost/python/extract.hpp</a></span></dt>
  25. <dd><dl>
  26. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.introduction">Introduction</a></span></dt>
  27. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract">Class
  28. template <code class="computeroutput"><span class="identifier">extract</span></code></a></span></dt>
  29. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_construct">Class
  30. template <code class="computeroutput"><span class="identifier">extract</span></code> constructors
  31. and destructor</a></span></dt>
  32. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_observer_">Class
  33. template <code class="computeroutput"><span class="identifier">extract</span></code> observer
  34. functions</a></span></dt>
  35. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.example">Example</a></span></dt>
  36. </dl></dd>
  37. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_implicit_hpp.html">boost/python/implicit.hpp</a></span></dt>
  38. <dd><dl>
  39. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.introduction">Introduction</a></span></dt>
  40. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.function_template_implicit_conve">Function
  41. template <code class="computeroutput"><span class="identifier">implicit_convertible</span></code></a></span></dt>
  42. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.example">Example</a></span></dt>
  43. </dl></dd>
  44. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_lvalue_from_pytype_.html">boost/python/lvalue_from_pytype.hpp</a></span></dt>
  45. <dd><dl>
  46. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.introduction">Introduction</a></span></dt>
  47. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_lvalue_from_pytyp">Class
  48. template <code class="computeroutput"><span class="identifier">lvalue_from_pytype</span></code></a></span></dt>
  49. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_identity">Class
  50. template <code class="computeroutput"><span class="identifier">extract_identity</span></code></a></span></dt>
  51. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.class_template_extract_member">Class
  52. template <code class="computeroutput"><span class="identifier">extract_member</span></code></a></span></dt>
  53. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_lvalue_from_pytype_.html#to_from_python_type_conversion.boost_python_lvalue_from_pytype_.example">Example</a></span></dt>
  54. </dl></dd>
  55. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_opaque_pointer_conv.html">boost/python/opaque_pointer_converter.hpp</a></span></dt>
  56. <dd><dl>
  57. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_opaque_pointer_conv.html#to_from_python_type_conversion.boost_python_opaque_pointer_conv.introduction">Introduction</a></span></dt>
  58. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_opaque_pointer_conv.html#to_from_python_type_conversion.boost_python_opaque_pointer_conv.class_template_opaque_constructo">Class
  59. template <code class="computeroutput"><span class="identifier">opaque</span></code> constructor</a></span></dt>
  60. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_opaque_pointer_conv.html#to_from_python_type_conversion.boost_python_opaque_pointer_conv.macro_boost_python_opaque_specia">Macro
  61. <code class="computeroutput"><span class="identifier">BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID</span><span class="special">(</span><span class="identifier">Pointee</span><span class="special">)</span></code></a></span></dt>
  62. </dl></dd>
  63. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_to_python_converter.html">boost/python/to_python_converter.hpp</a></span></dt>
  64. <dd><dl>
  65. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.introduction">Introduction</a></span></dt>
  66. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert">Class
  67. template <code class="computeroutput"><span class="identifier">to_python_converter</span></code></a></span></dt>
  68. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_to_python_converter.html#to_from_python_type_conversion.boost_python_to_python_converter.example">Example</a></span></dt>
  69. </dl></dd>
  70. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html">boost/python/register_ptr_to_python.hpp</a></span></dt>
  71. <dd><dl>
  72. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html#to_from_python_type_conversion.boost_python_register_ptr_to_pyt.introduction">Introduction</a></span></dt>
  73. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html#to_from_python_type_conversion.boost_python_register_ptr_to_pyt.function_register_ptr_to_python">Function
  74. <code class="computeroutput"><span class="identifier">register_ptr_to_python</span></code></a></span></dt>
  75. <dt><span class="section"><a href="to_from_python_type_conversion/boost_python_register_ptr_to_pyt.html#to_from_python_type_conversion.boost_python_register_ptr_to_pyt.example">Example</a></span></dt>
  76. </dl></dd>
  77. </dl>
  78. </div>
  79. <div class="section">
  80. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  81. <a name="to_from_python_type_conversion.boost_python_extract_hpp"></a><a class="link" href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp" title="boost/python/extract.hpp">boost/python/extract.hpp</a>
  82. </h2></div></div></div>
  83. <div class="toc"><dl class="toc">
  84. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.introduction">Introduction</a></span></dt>
  85. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract">Class
  86. template <code class="computeroutput"><span class="identifier">extract</span></code></a></span></dt>
  87. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_construct">Class
  88. template <code class="computeroutput"><span class="identifier">extract</span></code> constructors
  89. and destructor</a></span></dt>
  90. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_observer_">Class
  91. template <code class="computeroutput"><span class="identifier">extract</span></code> observer
  92. functions</a></span></dt>
  93. <dt><span class="section"><a href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.example">Example</a></span></dt>
  94. </dl></div>
  95. <div class="section">
  96. <div class="titlepage"><div><div><h3 class="title">
  97. <a name="to_from_python_type_conversion.boost_python_extract_hpp.introduction"></a><a class="link" href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.introduction" title="Introduction">Introduction</a>
  98. </h3></div></div></div>
  99. <p>
  100. Exposes a mechanism for extracting C++ object values from generalized Python
  101. objects. Note that <code class="computeroutput"><span class="identifier">extract</span><span class="special">&lt;...&gt;</span></code> can also be used to "downcast"
  102. an <a class="link" href="object_wrappers/boost_python_object_hpp.html#object_wrappers.boost_python_object_hpp.class_object" title="Class object"><code class="computeroutput"><span class="identifier">object</span></code></a> to some specific <a class="link" href="concepts/objectwrapper.html" title="ObjectWrapper">ObjectWrapper</a>. Because invoking
  103. a mutable python type with an argument of the same type (e.g. <code class="computeroutput"><span class="identifier">list</span><span class="special">([</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">]</span></code>) typically makes a copy of the argument
  104. object, this may be the only way to access the <a class="link" href="concepts/objectwrapper.html" title="ObjectWrapper">ObjectWrapper</a>'s
  105. interface on the original object.
  106. </p>
  107. </div>
  108. <div class="section">
  109. <div class="titlepage"><div><div><h3 class="title">
  110. <a name="to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract"></a><a class="link" href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract" title="Class template extract">Class
  111. template <code class="computeroutput"><span class="identifier">extract</span></code></a>
  112. </h3></div></div></div>
  113. <p>
  114. <code class="computeroutput"><span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
  115. can be used to extract a value of an arbitrary C++ type from an instance
  116. of <a class="link" href="object_wrappers/boost_python_object_hpp.html#object_wrappers.boost_python_object_hpp.class_object" title="Class object">object</a>.
  117. Two usages are supported:
  118. </p>
  119. <div class="orderedlist"><ol class="orderedlist" type="1">
  120. <li class="listitem">
  121. <code class="computeroutput"><span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;(</span><span class="identifier">o</span><span class="special">)</span></code>
  122. is a temporary object which is implicitly convertible to <code class="computeroutput"><span class="identifier">T</span></code> (explicit conversion is also available
  123. through the object's function-call operator). However, if no conversion
  124. is available which can convert o to an object of type <code class="computeroutput"><span class="identifier">T</span></code>, a Python TypeError exception will
  125. be raised.
  126. </li>
  127. <li class="listitem">
  128. <code class="computeroutput"><span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">o</span><span class="special">);</span></code>
  129. constructs an extractor whose <code class="computeroutput"><span class="identifier">check</span><span class="special">()</span></code> member function can be used to ask
  130. whether a conversion is available without causing an exception to be
  131. thrown.
  132. </li>
  133. </ol></div>
  134. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">python</span>
  135. <span class="special">{</span>
  136. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
  137. <span class="keyword">struct</span> <span class="identifier">extract</span>
  138. <span class="special">{</span>
  139. <span class="keyword">typedef</span> <span class="identifier">unspecified</span> <span class="identifier">result_type</span><span class="special">;</span>
  140. <span class="identifier">extract</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*);</span>
  141. <span class="identifier">extract</span><span class="special">(</span><span class="identifier">object</span> <span class="keyword">const</span><span class="special">&amp;);</span>
  142. <span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()()</span> <span class="keyword">const</span><span class="special">;</span>
  143. <span class="keyword">operator</span> <span class="identifier">result_type</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
  144. <span class="keyword">bool</span> <span class="identifier">check</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
  145. <span class="special">};</span>
  146. <span class="special">}}</span>
  147. </pre>
  148. </div>
  149. <div class="section">
  150. <div class="titlepage"><div><div><h3 class="title">
  151. <a name="to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_construct"></a><a class="link" href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_construct" title="Class template extract constructors and destructor">Class
  152. template <code class="computeroutput"><span class="identifier">extract</span></code> constructors
  153. and destructor</a>
  154. </h3></div></div></div>
  155. <pre class="programlisting"><span class="identifier">extract</span><span class="special">(</span><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">p</span><span class="special">);</span>
  156. <span class="identifier">extract</span><span class="special">(</span><span class="identifier">object</span> <span class="keyword">const</span><span class="special">&amp;);</span>
  157. </pre>
  158. <div class="variablelist">
  159. <p class="title"><b></b></p>
  160. <dl class="variablelist">
  161. <dt><span class="term">Requires</span></dt>
  162. <dd><p>
  163. The first form requires that p is non-null.
  164. </p></dd>
  165. <dt><span class="term">Effects</span></dt>
  166. <dd><p>
  167. Stores a pointer to the Python object managed by its constructor
  168. argument. In particular, the reference count of the object is not
  169. incremented. The onus is on the user to be sure it is not destroyed
  170. before the extractor's conversion function is called.
  171. </p></dd>
  172. </dl>
  173. </div>
  174. </div>
  175. <div class="section">
  176. <div class="titlepage"><div><div><h3 class="title">
  177. <a name="to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_observer_"></a><a class="link" href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract_observer_" title="Class template extract observer functions">Class
  178. template <code class="computeroutput"><span class="identifier">extract</span></code> observer
  179. functions</a>
  180. </h3></div></div></div>
  181. <pre class="programlisting"><span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()()</span> <span class="keyword">const</span><span class="special">;</span>
  182. <span class="keyword">operator</span> <span class="identifier">result_type</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
  183. </pre>
  184. <div class="variablelist">
  185. <p class="title"><b></b></p>
  186. <dl class="variablelist">
  187. <dt><span class="term">Effects</span></dt>
  188. <dd><p>
  189. Converts the stored pointer to result_type, which is either T or
  190. T const&amp;.
  191. </p></dd>
  192. <dt><span class="term">Returns</span></dt>
  193. <dd><p>
  194. An object of result_type corresponding to the one referenced by the
  195. stored pointer.
  196. </p></dd>
  197. <dt><span class="term">Throws</span></dt>
  198. <dd><p>
  199. <a class="link" href="high_level_components/boost_python_errors_hpp.html#high_level_components.boost_python_errors_hpp.class_error_already_set" title="Class error_already_set"><code class="computeroutput"><span class="identifier">error_already_set</span></code></a> and sets
  200. a <code class="computeroutput"><span class="identifier">TypeError</span></code> if no
  201. such conversion is available. May also emit other unspecified exceptions
  202. thrown by the converter which is actually used.
  203. </p></dd>
  204. </dl>
  205. </div>
  206. <pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">check</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span></pre>
  207. <div class="variablelist">
  208. <p class="title"><b></b></p>
  209. <dl class="variablelist">
  210. <dt><span class="term">Postconditions</span></dt>
  211. <dd><p>
  212. None. In particular, note that a return value of true does not preclude
  213. an exception being thrown from operator result_type() or operator()().
  214. </p></dd>
  215. <dt><span class="term">Returns</span></dt>
  216. <dd><p>
  217. false only if no conversion from the stored pointer to T is available.
  218. </p></dd>
  219. </dl>
  220. </div>
  221. </div>
  222. <div class="section">
  223. <div class="titlepage"><div><div><h3 class="title">
  224. <a name="to_from_python_type_conversion.boost_python_extract_hpp.example"></a><a class="link" href="to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.example" title="Example">Example</a>
  225. </h3></div></div></div>
  226. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cstdio</span><span class="special">&gt;</span>
  227. <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>
  228. <span class="keyword">int</span> <span class="identifier">Print</span><span class="special">(</span><span class="identifier">str</span> <span class="identifier">s</span><span class="special">)</span>
  229. <span class="special">{</span>
  230. <span class="comment">// extract a C string from the Python string object</span>
  231. <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">c_str</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*&gt;(</span><span class="identifier">s</span><span class="special">);</span>
  232. <span class="comment">// Print it using printf</span>
  233. <span class="identifier">std</span><span class="special">::</span><span class="identifier">printf</span><span class="special">(</span><span class="string">"%s\n"</span><span class="special">,</span> <span class="identifier">c_str</span><span class="special">);</span>
  234. <span class="comment">// Get the Python string's length and convert it to an int</span>
  235. <span class="keyword">return</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"__len__"</span><span class="special">)())</span>
  236. <span class="special">}</span>
  237. </pre>
  238. <p>
  239. The following example shows how extract can be used along with <a class="link" href="high_level_components.html#high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel" title="Class template class_&lt;T, Bases, HeldType, NonCopyable&gt;"><code class="computeroutput"><span class="identifier">class_</span><span class="special">&lt;...&gt;</span></code></a>
  240. to create and access an instance of a wrapped C++ class.
  241. </p>
  242. <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">X</span>
  243. <span class="special">{</span>
  244. <span class="identifier">X</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">v</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span>
  245. <span class="keyword">int</span> <span class="identifier">value</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">v</span><span class="special">;</span> <span class="special">}</span>
  246. <span class="keyword">private</span><span class="special">:</span>
  247. <span class="keyword">int</span> <span class="identifier">v</span><span class="special">;</span>
  248. <span class="special">};</span>
  249. <span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">extract_ext</span><span class="special">)</span>
  250. <span class="special">{</span>
  251. <span class="identifier">object</span> <span class="identifier">x_class</span><span class="special">(</span>
  252. <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;(</span><span class="string">"X"</span><span class="special">,</span> <span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;())</span>
  253. <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"value"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">value</span><span class="special">))</span>
  254. <span class="special">;</span>
  255. <span class="comment">// Instantiate an X object through the Python interface. </span>
  256. <span class="comment">// Its lifetime is now managed by x_obj.</span>
  257. <span class="identifier">object</span> <span class="identifier">x_obj</span> <span class="special">=</span> <span class="identifier">x_class</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
  258. <span class="comment">// Get a reference to the C++ object out of the Python object</span>
  259. <span class="identifier">X</span><span class="special">&amp;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&amp;&gt;(</span><span class="identifier">x_obj</span><span class="special">);</span>
  260. <span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">value</span><span class="special">()</span> <span class="special">==</span> <span class="number">3</span><span class="special">);</span>
  261. <span class="special">}</span>
  262. </pre>
  263. </div>
  264. </div>
  265. </div>
  266. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  267. <td align="left"></td>
  268. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
  269. Distributed under the Boost Software License, Version 1.0. (See accompanying
  270. 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>
  271. </p>
  272. </div></td>
  273. </tr></table>
  274. <hr>
  275. <div class="spirit-nav">
  276. <a accesskey="p" href="function_invocation_and_creation/models_of_resultconvertergenerat.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="to_from_python_type_conversion/boost_python_implicit_hpp.html"><img src="../images/next.png" alt="Next"></a>
  277. </div>
  278. </body>
  279. </html>