1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>boost/python/make_function.hpp</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="../function_invocation_and_creation.html" title="Chapter 4. Function Invocation and Creation"> 9<link rel="prev" href="boost_python_data_members_hpp.html" title="boost/python/data_members.hpp"> 10<link rel="next" href="boost_python_overloads_hpp.html" title="boost/python/overloads.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="boost_python_data_members_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.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="boost_python_overloads_hpp.html"><img src="../../images/next.png" alt="Next"></a> 17</div> 18<div class="section"> 19<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 20<a name="function_invocation_and_creation.boost_python_make_function_hpp"></a><a class="link" href="boost_python_make_function_hpp.html" title="boost/python/make_function.hpp">boost/python/make_function.hpp</a> 21</h2></div></div></div> 22<div class="toc"><dl class="toc"> 23<dt><span class="section"><a href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.introduction">Introduction</a></span></dt> 24<dt><span class="section"><a href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions">Functions</a></span></dt> 25<dt><span class="section"><a href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.example">Example</a></span></dt> 26</dl></div> 27<div class="section"> 28<div class="titlepage"><div><div><h3 class="title"> 29<a name="function_invocation_and_creation.boost_python_make_function_hpp.introduction"></a><a class="link" href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.introduction" title="Introduction">Introduction</a> 30</h3></div></div></div> 31<p> 32 make_function() and make_constructor() are the functions used internally 33 by def() and class_<>::def() to produce Python callable objects which 34 wrap C++ functions and member functions. 35 </p> 36</div> 37<div class="section"> 38<div class="titlepage"><div><div><h3 class="title"> 39<a name="function_invocation_and_creation.boost_python_make_function_hpp.functions"></a><a class="link" href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.functions" title="Functions">Functions</a> 40</h3></div></div></div> 41<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">></span> 42<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">)</span> 43 44<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">></span> 45<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">policies</span><span class="special">)</span> 46 47<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">KeywordsOrSignature</span><span class="special">></span> 48<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">KeywordsOrSignature</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">ks</span><span class="special">)</span> 49 50<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Signature</span><span class="special">></span> 51<span class="identifier">object</span> <span class="identifier">make_function</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">kw</span><span class="special">,</span> <span class="identifier">Signature</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">sig</span><span class="special">)</span> 52</pre> 53<div class="variablelist"> 54<p class="title"><b></b></p> 55<dl class="variablelist"> 56<dt><span class="term">Requires</span></dt> 57<dd><p> 58 F is a function pointer or member function pointer type. If policies 59 are supplied, it must be a model of CallPolicies. If kewords are 60 supplied, it must be the result of a keyword-expression specifying 61 no more arguments than the arity of f. 62 </p></dd> 63<dt><span class="term">Effects</span></dt> 64<dd> 65<p> 66 Creates a Python callable object which, when called from Python, 67 converts its arguments to C++ and calls f. If F is a pointer-to-member-function 68 type, the target object of the function call (*this) will be taken 69 from the first Python argument, and subsequent Python arguments will 70 be used as the arguments to f. 71 </p> 72<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 73<li class="listitem"> 74 If policies are supplied, it will be applied to the function 75 as described here. 76 </li> 77<li class="listitem"> 78 If keywords are supplied, the keywords will be applied in order 79 to the final arguments of the resulting function. 80 </li> 81<li class="listitem"> 82 If Signature is supplied, it should be an instance of an MPL 83 front-extensible sequence representing the function's return 84 type followed by its argument types. Pass a Signature when wrapping 85 function object types whose signatures can't be deduced, or when 86 you wish to override the types which will be passed to the wrapped 87 function. 88 </li> 89</ul></div> 90</dd> 91<dt><span class="term">Returns</span></dt> 92<dd><p> 93 An instance of object which holds the new Python callable object. 94 </p></dd> 95<dt><span class="term">Caveats</span></dt> 96<dd><p> 97 An argument of pointer type may be 0 if None is passed from Python. 98 An argument type which is a constant reference may refer to a temporary 99 which was created from the Python object for just the duration of 100 the call to the wrapped function, for example a std::vector conjured 101 up by the conversion process from a Python list. Use a non-const 102 reference argument when a persistent lvalue is required. 103 </p></dd> 104</dl> 105</div> 106<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">></span> 107<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">)</span> 108 109<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">></span> 110<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">policies</span><span class="special">)</span> 111 112<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">KeywordsOrSignature</span><span class="special">></span> 113<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">KeywordsOrSignature</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">ks</span><span class="special">)</span> 114 115<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">F</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Policies</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Keywords</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Signature</span><span class="special">></span> 116<span class="identifier">object</span> <span class="identifier">make_constructor</span><span class="special">(</span><span class="identifier">F</span> <span class="identifier">f</span><span class="special">,</span> <span class="identifier">Policies</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">policies</span><span class="special">,</span> <span class="identifier">Keywords</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">kw</span><span class="special">,</span> <span class="identifier">Signature</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">sig</span><span class="special">)</span> 117</pre> 118<div class="variablelist"> 119<p class="title"><b></b></p> 120<dl class="variablelist"> 121<dt><span class="term">Requires</span></dt> 122<dd><p> 123 F is a function pointer type. If policies are supplied, it must be 124 a model of CallPolicies. If kewords are supplied, it must be the 125 result of a keyword-expression specifying no more arguments than 126 the arity of f. 127 </p></dd> 128<dt><span class="term">Effects</span></dt> 129<dd><p> 130 Creates a Python callable object which, when called from Python, 131 converts its arguments to C++ and calls f. 132 </p></dd> 133<dt><span class="term">Returns</span></dt> 134<dd><p> 135 An instance of object which holds the new Python callable object. 136 </p></dd> 137</dl> 138</div> 139</div> 140<div class="section"> 141<div class="titlepage"><div><div><h3 class="title"> 142<a name="function_invocation_and_creation.boost_python_make_function_hpp.example"></a><a class="link" href="boost_python_make_function_hpp.html#function_invocation_and_creation.boost_python_make_function_hpp.example" title="Example">Example</a> 143</h3></div></div></div> 144<p> 145 C++ function exposed below returns a callable object wrapping one of two 146 functions. 147 </p> 148<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">make_function</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 149<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 150 151<span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">foo</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="string">"foo"</span><span class="special">;</span> <span class="special">}</span> 152<span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">bar</span><span class="special">()</span> <span class="special">{</span> <span class="keyword">return</span> <span class="string">"bar"</span><span class="special">;</span> <span class="special">}</span> 153 154<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> 155<span class="identifier">object</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="keyword">bool</span> <span class="identifier">selector</span><span class="special">)</span> 156<span class="special">{</span> 157 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">selector</span><span class="special">)</span> 158 <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">make_function</span><span class="special">(</span><span class="identifier">foo</span><span class="special">);</span> 159 <span class="keyword">else</span> 160 <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">make_function</span><span class="special">(</span><span class="identifier">bar</span><span class="special">);</span> 161<span class="special">}</span> 162 163<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">make_function_test</span><span class="special">)</span> 164<span class="special">{</span> 165 <span class="identifier">def</span><span class="special">(</span><span class="string">"choose_function"</span><span class="special">,</span> <span class="identifier">choose_function</span><span class="special">);</span> 166<span class="special">}</span> 167</pre> 168<p> 169 It can be used this way in Python: 170 </p> 171<pre class="programlisting"><span class="special">>>></span> <span class="identifier">from</span> <span class="identifier">make_function_test</span> <span class="identifier">import</span> <span class="special">*</span> 172<span class="special">>>></span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> 173<span class="special">>>></span> <span class="identifier">g</span> <span class="special">=</span> <span class="identifier">choose_function</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> 174<span class="special">>>></span> <span class="identifier">f</span><span class="special">()</span> 175<span class="char">'foo'</span> 176<span class="special">>>></span> <span class="identifier">g</span><span class="special">()</span> 177<span class="char">'bar'</span> 178</pre> 179</div> 180</div> 181<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 182<td align="left"></td> 183<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p> 184 Distributed under the Boost Software License, Version 1.0. (See accompanying 185 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> 186 </p> 187</div></td> 188</tr></table> 189<hr> 190<div class="spirit-nav"> 191<a accesskey="p" href="boost_python_data_members_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.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="boost_python_overloads_hpp.html"><img src="../../images/next.png" alt="Next"></a> 192</div> 193</body> 194</html> 195