mpl/doc/tutorial/the-lambda-metafunction.html
2007-11-03 03:25:13 +00:00

89 lines
6.3 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" />
<title>THE BOOST MPL LIBRARY: The lambda Metafunction</title>
<link rel="stylesheet" href="../style.css" type="text/css" />
</head>
<body class="docframe">
<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./handling-placeholders.html" class="navigation-link">Prev</a>&nbsp;<a href="./the-apply-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./the-apply-metafunction.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./handling-placeholders.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
<td class="header-group page-location"><a href="../index.html" class="navigation-link">Front Page</a> / <a href="./tutorial-metafunctions.html" class="navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming</a> / <a href="./handling-placeholders.html" class="navigation-link">Handling Placeholders</a> / <a href="./the-lambda-metafunction.html" class="navigation-link">The lambda Metafunction</a></td>
</tr></table><div class="header-separator"></div>
<div class="section" id="the-lambda-metafunction">
<h1><a class="toc-backref" href="./handling-placeholders.html#id49" name="the-lambda-metafunction">The <tt class="literal"><span class="pre">lambda</span></tt> Metafunction</a></h1>
<p>We can <em>generate</em> a metafunction class from
<tt class="literal"><span class="pre">boost::add_pointer&lt;_1&gt;</span></tt>, using MPL's <tt class="literal"><span class="pre">lambda</span></tt> metafunction:</p>
<pre class="literal-block">
template &lt;class X&gt;
struct two_pointers
: twice&lt;<strong>typename mpl::lambda&lt;boost::add_pointer&lt;_1&gt; &gt;::type</strong>, X&gt;
{};
BOOST_STATIC_ASSERT((
boost::is_same&lt;
two_pointers&lt;int&gt;::type
, int**
&gt;::value
));
</pre>
<!-- @ prefix.append('#include <boost/mpl/lambda.hpp>')
compile('all') -->
<p>We'll refer to metafunction classes like <tt class="literal"><span class="pre">add_pointer_f</span></tt> and
placeholder expressions like <tt class="literal"><span class="pre">boost::add_pointer&lt;_1&gt;</span></tt>
as <strong>lambda expressions</strong>. The term, meaning &quot;unnamed function
object,&quot; was introduced in the 1930s by the logician Alonzo Church
as part of a fundamental theory of computation he called the
<em>lambda-calculus</em>. <a class="footnote-reference" href="#lambda" id="id10" name="id10">[4]</a> MPL uses the somewhat obscure word
<tt class="literal"><span class="pre">lambda</span></tt> because of its well-established precedent in functional
programming languages.</p>
<table class="footnote" frame="void" id="lambda" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id10" name="lambda">[4]</a></td><td>See <a class="reference" href="http://en.wikipedia.org/wiki/Lambda_calculus" target="_top">http://en.wikipedia.org/wiki/Lambda_calculus</a> for
an in-depth treatment, including a reference to Church's paper
proving that the equivalence of lambda expressions is in general
not decidable.</td></tr>
</tbody>
</table>
<p>Although its primary purpose is to turn placeholder expressions
into metafunction classes, <tt class="literal"><span class="pre">mpl::lambda</span></tt> can accept any lambda
expression, even if it's already a metafunction class. In that
case, <tt class="literal"><span class="pre">lambda</span></tt> returns its argument unchanged. MPL algorithms
like <tt class="literal"><span class="pre">transform</span></tt> call <tt class="literal"><span class="pre">lambda</span></tt> internally, before invoking the
resulting metafunction class, so that they work equally well with
either kind of lambda expression. We can apply the same strategy
to <tt class="literal"><span class="pre">twice</span></tt>:</p>
<pre class="literal-block">
template &lt;class F, class X&gt;
struct twice
: apply1&lt;
typename mpl::lambda&lt;F&gt;::type
, typename apply1&lt;
typename mpl::lambda&lt;F&gt;::type
, X
&gt;::type
&gt;
{};
</pre>
<p>Now we can use <tt class="literal"><span class="pre">twice</span></tt> with metafunction classes <em>and</em>
placeholder expressions:</p>
<pre class="literal-block">
int* x;
twice&lt;<strong>add_pointer_f</strong>, int&gt;::type p = &amp;x;
twice&lt;<strong>boost::add_pointer&lt;_1&gt;</strong>, int&gt;::type q = &amp;x;
</pre>
<!-- @ stack[-2:] = [ apply1, stack[-2], add_pointer_f, stack[-1]]
compile('all') -->
</div>
<div class="footer-separator"></div>
<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./handling-placeholders.html" class="navigation-link">Prev</a>&nbsp;<a href="./the-apply-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./the-apply-metafunction.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./handling-placeholders.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
</tr></table></body>
</html>