mirror of
https://github.com/boostorg/mpl.git
synced 2026-01-26 17:02:20 +01:00
77 lines
3.4 KiB
HTML
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> Expression </th><th> Expression type </th></tr>
|
|
<tr><td><code>typename f::type</code></td><td>A type </td></tr>
|
|
<tr><td><code>typename f<a1,..,an>::type</code></td><td>A type </td></tr>
|
|
</table>
|
|
<p>
|
|
<h3>Expression semantics</h3>
|
|
<p>
|
|
<table border="1">
|
|
<tr><th> Expression </th><th> Complexity </th><th> Precondition </th><th> Semantics </th><th> Postcondition </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<a1,..,an>::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<a1,..,an>::type</code> is a <em>type-name</em></td><td><code>f<a1,..,an>::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< typename T > struct sizeof_
|
|
{
|
|
typedef int_< sizeof(T) > type;
|
|
};
|
|
<p>
|
|
<span class="cxx-comment">// binary metafunction</span>
|
|
template< typename T1, typename T2 >
|
|
struct is_same
|
|
{
|
|
typedef false_ type;
|
|
};
|
|
<p>
|
|
template< typename T >
|
|
struct is_same<T,T>
|
|
{
|
|
typedef true_ type;
|
|
};
|
|
<p>
|
|
<span class="cxx-comment">// invocations</span>
|
|
typedef always_true::type t1;
|
|
typedef sizeof_<int>::type t2;
|
|
typedef is_same<int,char>::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> |