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