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