1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chapter 35. Boost.Typeof</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="boost_typeerasure/related.html" title="Related Work">
<link rel="next" href="typeof/tuto.html" title="Tutorial">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_typeerasure/related.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="typeof/tuto.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="typeof"></a>Chapter 35. Boost.Typeof</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Arkadiy</span> <span class="surname">Vertleyb</span>
</h3></div></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Peder</span> <span class="surname">Holt</span>
</h3></div></div>
<div><p class="copyright">Copyright © 2004, 2005 Arkadiy Vertleyb, Peder Holt</p></div>
<div><div class="legalnotice">
<a name="typeof.legal"></a><p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>)
</p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="typeof.html#typeof.moti">Motivation</a></span></dt>
<dt><span class="section"><a href="typeof/tuto.html">Tutorial</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html">Reference</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="typeof/refe.html#typeof.auto">AUTO, AUTO_TPL</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.compl">COMPLIANT</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.incr">INCREMENT_REGISTRATION_GROUP</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.inte">INTEGRAL</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.limit_func">LIMIT_FUNCTION_ARITY</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.messages">MESSAGES</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.limit_size">LIMIT_SIZE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.regtype">REGISTER_TYPE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.regtemp">REGISTER_TEMPLATE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.temp">TEMPLATE</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.typo">TYPEOF, TYPEOF_TPL</a></span></dt>
<dt><span class="section"><a href="typeof/refe.html#typeof.typn">TYPEOF_NESTED_TYPEDEF, TYPEOF_NESTED_TYPEDEF_TPL</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="typeof/other.html">Other considerations and tips</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="typeof/other.html#typeof.natem">Native typeof support and emulation</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.parties">The three participating parties</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.features">Supported features</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.what">What needs to be registered?</a></span></dt>
<dt><span class="section"><a href="typeof/other.html#typeof.limi">Limitations</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="typeof/cont.html">Contributed By:</a></span></dt>
<dt><span class="section"><a href="typeof/ackn.html">Acknowledgements</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="typeof.moti"></a>Motivation</h2></div></div></div>
<p>
Today many template libraries supply object generators to simplify object creation
by utilizing the C++ template argument deduction facility. Consider <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></code>.
In order to instantiate this class template and create a temporary object of
this instantiation, one has to supply template parameters, as well as parameters
to the constructor:
</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">>(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
To avoid this duplication, STL supplies the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span></code>
object generator. When it is used, the types of template parameters are deduced
from supplied function arguments:
</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
For the temporary objects it is enough. However, when a named object needs
to be allocated, the problem appears again:
</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">></span> <span class="identifier">p</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
The object generator no longer helps:
</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">></span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
</pre>
<p>
It would be nice to deduce the type of the object (on the left) from the expression
it is initialized with (on the right), but the current C++ syntax does not
allow for this.
</p>
<p>
The above example demonstrates the essence of the problem but does not demonstrate
its scale. Many libraries, especially expression template libraries, create
objects of really complex types, and go a long way to hide this complexity
behind object generators. Consider a nit Boost.Lambda functor:
</p>
<pre class="programlisting"><span class="identifier">_1</span> <span class="special">></span> <span class="number">15</span> <span class="special">&&</span> <span class="identifier">_2</span> <span class="special"><</span> <span class="number">20</span>
</pre>
<p>
If one wanted to allocate a named copy of such an innocently looking functor,
she would have to specify something like this:
</p>
<pre class="programlisting"><span class="identifier">lambda_functor</span><span class="special"><</span>
<span class="identifier">lambda_functor_base</span><span class="special"><</span>
<span class="identifier">logical_action</span><span class="special"><</span><span class="identifier">and_action</span><span class="special">>,</span>
<span class="identifier">tuple</span><span class="special"><</span>
<span class="identifier">lambda_functor</span><span class="special"><</span>
<span class="identifier">lambda_functor_base</span><span class="special"><</span>
<span class="identifier">relational_action</span><span class="special"><</span><span class="identifier">greater_action</span><span class="special">>,</span>
<span class="identifier">tuple</span><span class="special"><</span>
<span class="identifier">lambda_functor</span><span class="special"><</span><span class="identifier">placeholder</span><span class="special"><</span><span class="number">1</span><span class="special">></span> <span class="special">>,</span>
<span class="keyword">int</span> <span class="keyword">const</span>
<span class="special">></span>
<span class="special">></span>
<span class="special">>,</span>
<span class="identifier">lambda_functor</span><span class="special"><</span>
<span class="identifier">lambda_functor_base</span><span class="special"><</span>
<span class="identifier">relational_action</span><span class="special"><</span><span class="identifier">less_action</span><span class="special">>,</span>
<span class="identifier">tuple</span><span class="special"><</span>
<span class="identifier">lambda_functor</span><span class="special"><</span><span class="identifier">placeholder</span><span class="special"><</span><span class="number">2</span><span class="special">></span> <span class="special">>,</span>
<span class="keyword">int</span> <span class="keyword">const</span>
<span class="special">></span>
<span class="special">></span>
<span class="special">></span>
<span class="special">></span>
<span class="special">></span>
<span class="special">></span>
<span class="identifier">f</span> <span class="special">=</span> <span class="identifier">_1</span> <span class="special">></span> <span class="number">15</span> <span class="special">&&</span> <span class="identifier">_2</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span>
</pre>
<p>
Not exactly elegant. To solve this problem (as well as some other problems),
the C++ standard committee is considering a few additions to the standard language,
such as <code class="computeroutput"><span class="identifier">typeof</span><span class="special">/</span><span class="keyword">decltype</span></code> and <code class="computeroutput"><span class="keyword">auto</span></code>
(see <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf" target="_top">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf</a>).
</p>
<p>
The <code class="computeroutput"><span class="identifier">typeof</span></code> operator (or <code class="computeroutput"><span class="keyword">decltype</span></code>, which is a slightly different flavor
of <code class="computeroutput"><span class="identifier">typeof</span></code>) allows one to determine
the type of an expression at compile time. Using <code class="computeroutput"><span class="identifier">typeof</span></code>,
the above example can be simplified drastically:
</p>
<pre class="programlisting"><span class="identifier">typeof</span><span class="special">(</span><span class="identifier">_1</span> <span class="special">></span> <span class="number">15</span> <span class="special">&&</span> <span class="identifier">_2</span> <span class="special"><</span> <span class="number">20</span><span class="special">)</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">_1</span> <span class="special">></span> <span class="number">15</span> <span class="special">&&</span> <span class="identifier">_2</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span>
</pre>
<p>
Much better, but some duplication still exists. The <code class="computeroutput"><span class="keyword">auto</span></code>
type solves the rest of the problem:
</p>
<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">_1</span> <span class="special">></span> <span class="number">15</span> <span class="special">&&</span> <span class="identifier">_2</span> <span class="special"><</span> <span class="number">20</span><span class="special">;</span>
</pre>
<p>
The purpose of the Boost.Typeof library is to provide a library-based solution,
which could be used until the language-based facility is added to the Standard
and becomes widely available.
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: September 21, 2013 at 21:17:00 +0100</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_typeerasure/related.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="typeof/tuto.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|