1<?xml version="1.0" encoding="utf-8" ?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4<!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost --> 5<!-- Software License, Version 1.0. (See accompanying --> 6<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> 7<head> 8<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 9<meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" /> 10<title>THE BOOST MPL LIBRARY: Details</title> 11<link rel="stylesheet" href="../style.css" type="text/css" /> 12</head> 13<body class="docframe"> 14<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./the-importance-of-being.html" class="navigation-link">Prev</a> <a href="./exercises.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Back</a> <a href="./exercises.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial-metafunctions.html" class="navigation-link">Up</a> <a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td> 15<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="./details.html" class="navigation-link">Details</a></td> 16</tr></table><div class="header-separator"></div> 17<div class="section" id="details"> 18<h1><a class="toc-backref" href="./tutorial-metafunctions.html#id59" name="details">Details</a></h1> 19<p>By now you should have a fairly complete view of the fundamental 20concepts and language of both template metaprogramming in general 21and of the Boost Metaprogramming Library. This section 22reviews the highlights.</p> 23<dl> 24<dt>Metafunction forwarding.</dt> 25<dd>The technique of using public derivation to 26supply the nested <tt class="literal"><span class="pre">type</span></tt> of a metafunction by accessing the one 27provided by its base class.</dd> 28<dt>Metafunction class.</dt> 29<dd>The most basic way to formulate a compile-time 30function so that it can be treated as polymorphic metadata; that 31is, as a type. A metafunction class is a class with a nested 32metafunction called <tt class="literal"><span class="pre">apply</span></tt>.</dd> 33<dt>MPL.</dt> 34<dd><p class="first">Most of this book's examples will use the Boost 35Metaprogramming Library. Like the Boost type traits headers, 36MPL 37headers follow a simple convention:</p> 38<pre class="literal-block"> 39#include <boost/mpl/<em>component-name</em>.hpp> 40</pre> 41<p class="last">If the component's name ends in an underscore, however, the 42corresponding MPL header name does not include the trailing 43underscore. For example, <tt class="literal"><span class="pre">mpl::bool_</span></tt> can be found in 44<tt class="literal"><span class="pre"><boost/mpl/bool.hpp></span></tt>. Where the library deviates from this 45convention, we'll be sure to point it out to you.</p> 46</dd> 47</dl> 48<!-- @ignore() --> 49<dl> 50<dt>Higher-order function.</dt> 51<dd>A function that operates on or returns a function. Making 52metafunctions polymorphic with other metadata is a key 53ingredient in higher-order metaprogramming.</dd> 54<dt>Lambda expression.</dt> 55<dd>Simply put, a lambda expression is callable metadata. Without 56some form of callable metadata, higher-order metafunctions 57would be impossible. Lambda expressions have two basic forms: 58<em>metafunction classes</em> and <em>placeholder expressions</em>.</dd> 59<dt>Placeholder expression.</dt> 60<dd><p class="first">A kind of lambda expression that, through the use of 61placeholders, enables in-place <em>partial metafunction 62application</em> and <em>metafunction composition</em>. As you will see 63throughout this book, these features give us the truly amazing 64ability to build up almost any kind of complex type computation 65from more primitive metafunctions, right at its point of use:</p> 66<pre class="literal-block"> 67// find the position of a type x in some_sequence such that: 68// x is convertible to 'int' 69// && x is not 'char' 70// && x is not a floating type 71typedef mpl::find_if< 72 some_sequence 73 , mpl::and_< 74 boost::is_convertible<_1,int> 75 , mpl::not_<boost::is_same<_1,char> > 76 , mpl::not_<boost::is_float<_1> > 77 > 78 >::type iter; 79</pre> 80<p class="last">Placeholder expressions make good on the promise of algorithm reuse 81without forcing us to write new metafunction classes. The 82corresponding capability is often sorely missed in the runtime 83world of the STL, since it is often much easier to write a loop 84by hand than it is to use standard algorithms, despite their 85correctness and efficiency advantages.</p> 86</dd> 87</dl> 88<!-- @ example.prepend(''' 89#include <boost/mpl/and.hpp> 90#include <boost/mpl/not.hpp> 91#include <boost/mpl/find_if.hpp> 92#include <boost/type_traits/is_convertible.hpp> 93#include <boost/type_traits/is_float.hpp> 94typedef mpl::vector<char, double, short, long> some_sequence; 95''') 96compile() --> 97<dl> 98<dt>The <tt class="literal"><span class="pre">lambda</span></tt> metafunction.</dt> 99<dd>A metafunction that transforms a lambda expression into a 100corresponding metafunction class. For detailed information on 101<tt class="literal"><span class="pre">lambda</span></tt> and the lambda evaluation process, 102please see the <a class="reference" href="./reference-manual.html">the MPL reference manual</a>.</dd> 103<dt>The <tt class="literal"><span class="pre">apply</span></tt> metafunction.</dt> 104<dd>A metafunction that invokes its first argument, which must be a 105lambda expression, on its remaining arguments. In general, to 106invoke a lambda expression, you should always pass it to 107<tt class="literal"><span class="pre">mpl::apply</span></tt> along with the arguments you want to apply it 108to in lieu of using <tt class="literal"><span class="pre">lambda</span></tt> and invoking the result "manually."</dd> 109<dt>Lazy evaluation.</dt> 110<dd>A strategy of delaying evaluation until a result is 111required, thereby avoiding any unnecessary computation and any 112associated unnecessary errors. Metafunctions are only invoked 113when we access their nested <tt class="literal"><span class="pre">::type</span></tt>s, so we can supply all 114of their arguments without performing any computation and 115delay evaluation to the last possible moment.</dd> 116</dl> 117</div> 118 119<div class="footer-separator"></div> 120<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./the-importance-of-being.html" class="navigation-link">Prev</a> <a href="./exercises.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./lambda-details.html" class="navigation-link">Back</a> <a href="./exercises.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial-metafunctions.html" class="navigation-link">Up</a> <a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td> 121</tr></table></body> 122</html> 123