7fc311d16f3a56ab79566a6f8d99e7f2ae7752a5
[platform/upstream/boost.git] / doc / html / typeof.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Chapter&#160;35.&#160;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&#160;I.&#160;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">
11 </head>
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>
20 </tr></table>
21 <hr>
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>
24 </div>
25 <div class="chapter">
26 <div class="titlepage"><div>
27 <div><h2 class="title">
28 <a name="typeof"></a>Chapter&#160;35.&#160;Boost.Typeof</h2></div>
29 <div><div class="author"><h3 class="author">
30 <span class="firstname">Arkadiy</span> <span class="surname">Vertleyb</span>
31 </h3></div></div>
32 <div><div class="author"><h3 class="author">
33 <span class="firstname">Peder</span> <span class="surname">Holt</span>
34 </h3></div></div>
35 <div><p class="copyright">Copyright &#169; 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>)
41       </p>
42 </div></div>
43 </div></div>
44 <div class="toc">
45 <p><b>Table of Contents</b></p>
46 <dl class="toc">
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>
50 <dd><dl>
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>
63 </dl></dd>
64 <dt><span class="section"><a href="typeof/other.html">Other considerations and tips</a></span></dt>
65 <dd><dl>
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>
71 </dl></dd>
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>
74 </dl>
75 </div>
76 <div class="section">
77 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
78 <a name="typeof.moti"></a>Motivation</h2></div></div></div>
79 <p>
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
84       to the constructor:
85     </p>
86 <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;(</span><span class="number">5</span><span class="special">,</span> <span class="number">3.14159</span><span class="special">);</span>
87 </pre>
88 <p>
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:
92     </p>
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>
94 </pre>
95 <p>
96       For the temporary objects it is enough. However, when a named object needs
97       to be allocated, the problem appears again:
98     </p>
99 <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</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>
100 </pre>
101 <p>
102       The object generator no longer helps:
103     </p>
104 <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</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>
105 </pre>
106 <p>
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
109       allow for this.
110     </p>
111 <p>
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:
116     </p>
117 <pre class="programlisting"><span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span>
118 </pre>
119 <p>
120       If one wanted to allocate a named copy of such an innocently looking functor,
121       she would have to specify something like this:
122     </p>
123 <pre class="programlisting"><span class="identifier">lambda_functor</span><span class="special">&lt;</span>
124     <span class="identifier">lambda_functor_base</span><span class="special">&lt;</span>
125         <span class="identifier">logical_action</span><span class="special">&lt;</span><span class="identifier">and_action</span><span class="special">&gt;,</span>
126         <span class="identifier">tuple</span><span class="special">&lt;</span>
127             <span class="identifier">lambda_functor</span><span class="special">&lt;</span>
128                 <span class="identifier">lambda_functor_base</span><span class="special">&lt;</span>
129                     <span class="identifier">relational_action</span><span class="special">&lt;</span><span class="identifier">greater_action</span><span class="special">&gt;,</span>
130                     <span class="identifier">tuple</span><span class="special">&lt;</span>
131                         <span class="identifier">lambda_functor</span><span class="special">&lt;</span><span class="identifier">placeholder</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
132                         <span class="keyword">int</span> <span class="keyword">const</span>
133                     <span class="special">&gt;</span>
134                 <span class="special">&gt;</span>
135             <span class="special">&gt;,</span>
136             <span class="identifier">lambda_functor</span><span class="special">&lt;</span>
137                 <span class="identifier">lambda_functor_base</span><span class="special">&lt;</span>
138                     <span class="identifier">relational_action</span><span class="special">&lt;</span><span class="identifier">less_action</span><span class="special">&gt;,</span>
139                     <span class="identifier">tuple</span><span class="special">&lt;</span>
140                         <span class="identifier">lambda_functor</span><span class="special">&lt;</span><span class="identifier">placeholder</span><span class="special">&lt;</span><span class="number">2</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
141                         <span class="keyword">int</span> <span class="keyword">const</span>
142                     <span class="special">&gt;</span>
143                 <span class="special">&gt;</span>
144             <span class="special">&gt;</span>
145         <span class="special">&gt;</span>
146     <span class="special">&gt;</span>
147 <span class="special">&gt;</span>
148 <span class="identifier">f</span> <span class="special">=</span> <span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span><span class="special">;</span>
149 </pre>
150 <p>
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>).
155     </p>
156 <p>
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:
161     </p>
162 <pre class="programlisting"><span class="identifier">typeof</span><span class="special">(</span><span class="identifier">_1</span> <span class="special">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</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">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span><span class="special">;</span>
163 </pre>
164 <p>
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:
167     </p>
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">&gt;</span> <span class="number">15</span> <span class="special">&amp;&amp;</span> <span class="identifier">_2</span> <span class="special">&lt;</span> <span class="number">20</span><span class="special">;</span>
169 </pre>
170 <p>
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.
174     </p>
175 </div>
176 </div>
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>
180 </tr></table>
181 <hr>
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>
184 </div>
185 </body>
186 </html>