Files
mpl/doc/ref/Metafunction.html
Aleksey Gurtovoy a69baa5eae update the docs
[SVN r17836]
2003-03-12 13:29:12 +00:00

77 lines
3.4 KiB
HTML

<!doctype html public "-//ietf//dtd html//en">
<html><head><title>boost::mpl::Metafunction</title>
<link rel="stylesheet" href="./mpl_wiki.css">
</head><body bgcolor="white">
<h1><a href="./Table_of_Contents.html"><img src="./mpl_logo.jpg" alt="[Home]" border=0 align="right"></a>Metafunction</h1><h3>Description</h3>
<p>
A <em>metafunction</em> is a class or a class template that represents a function invocable at compile-time. A non-nullary metafunction is invoked by instantiating the class template with particular template parameters
(metafunction arguments); the result of the metafunction application is accessible through the instantiation's nested <code>type</code> typedef. All metafunction's arguments must be types (i.e. only <em>type template parameters</em> are allowed). A metafunction can have a variable number of parameters. A nullary metafunction is represented as a class with a nested
<code>type</code> typename member.
<p>
<h3>Valid expressions</h3>
<p>
<table border="1">
<tr><th>&nbsp;Expression&nbsp;</th><th>&nbsp;Expression type &nbsp;</th></tr>
<tr><td><code>typename f::type</code></td><td>A type </td></tr>
<tr><td><code>typename f&lt;a1,..,an&gt;::type</code></td><td>A type </td></tr>
</table>
<p>
<h3>Expression semantics</h3>
<p>
<table border="1">
<tr><th>&nbsp;Expression&nbsp;</th><th>&nbsp;Complexity&nbsp;</th><th>&nbsp;Precondition&nbsp;</th><th>&nbsp;Semantics&nbsp;</th><th>&nbsp;Postcondition&nbsp;</th></tr>
<tr><td><code>typename f::type</code></td><td>unspecified</td><td><code>f</code> is a nullary metafunction; <code>f::type</code> is a <em>type-name</em></td><td><code>f::type</code> is the result of the metafunction invocation</td><td></td></tr>
<tr><td><code>typename f&lt;a1,..,an&gt;::type</code></td><td>unspecified</td><td><code>f</code> is an <code>n</code>-ary metafunction; <code>a1,..,an</code> are types; <code>f&lt;a1,..,an&gt;::type</code> is a <em>type-name</em></td><td><code>f&lt;a1,..,an&gt;::type</code> is the result of the metafunction invocation with the actual arguments <code>a1,..,an</code></td><td></td></tr>
</table>
<p>
<p>
<h3>Example</h3>
<p>
<pre>
<span class="cxx-comment">// nullary metafunction</span>
struct always_true { typedef true_ type; };
<p>
<span class="cxx-comment">// unary metafunction</span>
template&lt; typename T &gt; struct sizeof_
{
typedef int_&lt; sizeof(T) &gt; type;
};
<p>
<span class="cxx-comment">// binary metafunction</span>
template&lt; typename T1, typename T2 &gt;
struct is_same
{
typedef false_ type;
};
<p>
template&lt; typename T &gt;
struct is_same&lt;T,T&gt;
{
typedef true_ type;
};
<p>
<span class="cxx-comment">// invocations</span>
typedef always_true::type t1;
typedef sizeof_&lt;int&gt;::type t2;
typedef is_same&lt;int,char&gt;::type t3;
<p>
<span class="cxx-comment">// results checks</span>
BOOST_STATIC_ASSERT(t1::value);
BOOST_STATIC_ASSERT(t2::value == sizeof(int));
BOOST_STATIC_ASSERT(!t3::value);
</pre>
<p>
<h3>Models</h3>
<p>
<ul>
<li><code><a href="./Reference/plus.html">plus</a></code>
<li><code><a href="./Reference/not.html">not_</a></code>
<li><code><a href="./Reference/size.html">size</a></code>
<li><code><a href="./Reference/max_element.html">max_element</a></code>
</ul>
<p>
<h3>See also</h3>
<p>
<a href="./Metafunctions.html">Metafunctions</a>, <a href="./Metafunction_Class.html">Metafunction Class</a>
<p><hr>
<a href="./Table_of_Contents.html">Table of Contents</a><br>Last edited March 10, 2003 3:27 am</body></html>