1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
5 <title>Other considerations and tips</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="../typeof.html" title="Chapter 42. Boost.Typeof">
10 <link rel="prev" href="refe.html" title="Reference">
11 <link rel="next" href="cont.html" title="Contributed By:">
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>
23 <div class="spirit-nav">
24 <a accesskey="p" href="refe.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../typeof.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="cont.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
27 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
28 <a name="typeof.other"></a>Other considerations and tips</h2></div></div></div>
29 <div class="toc"><dl class="toc">
30 <dt><span class="section"><a href="other.html#typeof.natem">Native typeof support and emulation</a></span></dt>
31 <dt><span class="section"><a href="other.html#typeof.parties">The three participating parties</a></span></dt>
32 <dt><span class="section"><a href="other.html#typeof.features">Supported features</a></span></dt>
33 <dt><span class="section"><a href="other.html#typeof.what">What needs to be registered?</a></span></dt>
34 <dt><span class="section"><a href="other.html#typeof.limi">Limitations</a></span></dt>
37 <div class="titlepage"><div><div><h3 class="title">
38 <a name="typeof.natem"></a>Native typeof support and emulation</h3></div></div></div>
40 Many compilers support typeof already, most noticeable GCC and Metrowerks.
43 Igor Chesnokov discovered a method that allows to implement <code class="computeroutput"><span class="identifier">typeof</span></code> on the VC series of compilers. It
44 uses a bug in the Microsoft compiler that allows a nested class of base to
45 be defined in a class derived from base:
47 <pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">int</span> <span class="identifier">ID</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">typeof_access</span>
48 <span class="special">{</span>
49 <span class="keyword">struct</span> <span class="identifier">id2type</span><span class="special">;</span> <span class="comment">//not defined</span>
50 <span class="special">};</span>
52 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">ID</span><span class="special">></span> <span class="keyword">struct</span> <span class="identifier">typeof_register</span> <span class="special">:</span> <span class="identifier">typeof_access</span>
53 <span class="special">{</span>
54 <span class="comment">// define base's nested class here</span>
55 <span class="keyword">struct</span> <span class="identifier">typeof_access</span><span class="special">::</span><span class="identifier">id2type</span>
56 <span class="special">{</span>
57 <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">type</span><span class="special">;</span>
58 <span class="special">};</span>
59 <span class="special">};</span>
61 <span class="comment">//Type registration function </span>
62 <span class="identifier">typeof_register</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">compile</span><span class="special">-</span><span class="identifier">time</span><span class="special">-</span><span class="identifier">constant</span><span class="special">></span> <span class="identifier">register_type</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&);</span>
64 <span class="comment">//Actually register type by instantiating typeof_register for the correct type</span>
65 <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">register_type</span><span class="special">(</span><span class="identifier">some</span><span class="special">-</span><span class="identifier">type</span><span class="special">));</span>
67 <span class="comment">//Use the base class to access the type.</span>
68 <span class="keyword">typedef</span> <span class="identifier">typeof_access</span><span class="special">::</span><span class="identifier">id2type</span><span class="special">::</span><span class="identifier">type</span> <span class="identifier">type</span><span class="special">;</span>
71 Peder Holt adapted this method to VC7.0, where the nested class is a template
72 class that is specialized in the derived class.
75 In VC8.0, it seemed that all the bug-featire had been fixed, but Steven Watanabe
76 managed to implement a more rigorous version of the VC7.0 fix that enables
77 'typeof' to be supported 'natively' here as well.
80 For many other compilers neither native <code class="computeroutput"><span class="identifier">typeof</span></code>
81 support nor the trick described above is an option. For such compilers the
82 emulation method is the only way of implementing <code class="computeroutput"><span class="identifier">typeof</span></code>.
85 According to a rough estimate, at the time of this writing the introduction
86 of the <code class="computeroutput"><span class="identifier">typeof</span></code>, <code class="computeroutput"><span class="keyword">auto</span></code>, etc., into the C++ standard may not
87 happen soon. Even after it's done, some time still has to pass before most
88 compilers implement this feature. But even after that, there always are legacy
89 compilers to support (for example now, in 2005, many people are still using
90 VC6, long after VC7.x, and even VC8.0 beta became available).
93 Considering extreme usefulness of the feature right now, it seems to make
94 sense to implement it at the library level.
97 The emulation mode seems to be important even if a better option is present
98 on some particular compiler. If a library author wants to develop portable
99 code using <code class="computeroutput"><span class="identifier">typeof</span></code>, she needs
100 to use emulation mode and register her types and templates. Those users who
101 have a better option can still take advantage of it, since the registration
102 macros are defined as no-op on such compilers, while the users for whom emulation
103 is the only option will use it.
106 The other consideration applies to the users of VC7.1. Even though the more
107 convenient <code class="computeroutput"><span class="identifier">typeof</span></code> trick is
108 available, the possibility of upgrade to VC8, where emulation remains the
109 only option, should be considered.
112 The emulation mode can be forced on the compilers that don't use it by default
113 by defining the <code class="computeroutput"><span class="identifier">BOOST_TYPEOF_COMPLIANT</span></code>
116 <pre class="programlisting"><span class="identifier">g</span><span class="special">++</span> <span class="special">-</span><span class="identifier">D</span> <span class="identifier">BOOST_TYPEOF_COMPLIANT</span> <span class="special">-</span><span class="identifier">I</span> <span class="special">\</span><span class="identifier">boost</span><span class="special">\</span><span class="identifier">boost_1_32_0</span> <span class="identifier">main</span><span class="special">.</span><span class="identifier">cpp</span>
119 <div class="section">
120 <div class="titlepage"><div><div><h3 class="title">
121 <a name="typeof.parties"></a>The three participating parties</h3></div></div></div>
123 The Lambda example from the Motivation section requires the following registration:
125 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="identifier">BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP</span><span class="special">()</span>
127 <span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">tuples</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
128 <span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">lambda_functor</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span>
129 <span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">lambda_functor_base</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
130 <span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">relational_action</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span>
131 <span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">logical_action</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span>
132 <span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">other_action</span><span class="special">,</span> <span class="number">1</span><span class="special">);</span>
133 <span class="identifier">BOOST_TYPEOF_REGISTER_TYPE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">greater_action</span><span class="special">);</span>
134 <span class="identifier">BOOST_TYPEOF_REGISTER_TYPE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">less_action</span><span class="special">);</span>
135 <span class="identifier">BOOST_TYPEOF_REGISTER_TYPE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">and_action</span><span class="special">);</span>
136 <span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lambda</span><span class="special">::</span><span class="identifier">placeholder</span><span class="special">,</span> <span class="special">(</span><span class="keyword">int</span><span class="special">));</span>
139 It may seem that the price for the ability to discover the expression's type
140 is too high: rather large amount of registration is required. However note
141 that all of the above registration is done only once, and after that, any
142 combination of the registered types and templates would be handled. Moreover,
143 this registration is typically done not by the end-user, but rather by a
144 layer on top of some library (in this example -- Boost.Lambda).
147 When thinking about this, it's helpful to consider three parties: the typeof
148 facility, the library (probably built on expression templates principle),
149 and the end-user. The typeof facility is responsible for registering fundamental
150 types. The library can register its own types and templates.
153 In the best-case scenario, if the expressions always consist of only fundamental
154 types and library-defined types and templates, a library author can achieve
155 the impression that the <code class="computeroutput"><span class="identifier">typeof</span></code>
156 is natively supported for her library. On the other hand, the more often
157 expressions contain user-defined types, the more responsibility is put on
158 the end-user, and therefore the less attractive this approach becomes.
161 Thus, the ratio of user-defined types in the expressions should be the main
162 factor to consider when deciding whether or not to apply the typeof facility.
165 <div class="section">
166 <div class="titlepage"><div><div><h3 class="title">
167 <a name="typeof.features"></a>Supported features</h3></div></div></div>
169 The Typeof library pre-registers fundamental types. For these types, and
170 for any other types/templates registered by the user library or end-user,
171 any combination of the following is supported:
173 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
174 <li class="listitem">
177 <li class="listitem">
178 References (except top-level);
180 <li class="listitem">
181 Consts (except top-level);
183 <li class="listitem">
184 Volatiles (except top-level);
186 <li class="listitem">
189 <li class="listitem">
190 Functions, function pointers, and references;
192 <li class="listitem">
193 Pointers to member functions;
195 <li class="listitem">
196 Pointers to data members.
200 For example the following type:
202 <pre class="programlisting"><span class="keyword">int</span><span class="special">&</span> <span class="special">(*)(</span><span class="keyword">const</span> <span class="keyword">volatile</span> <span class="keyword">char</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="keyword">void</span><span class="special">(*)(</span><span class="keyword">short</span><span class="special">))</span>
205 is supported right away, and something like:
207 <pre class="programlisting"><span class="keyword">void</span> <span class="special">(</span><span class="identifier">MyClass</span><span class="special">::*)(</span><span class="keyword">int</span> <span class="identifier">MyClass</span><span class="special">::*,</span> <span class="identifier">MyClass</span><span class="special">[</span><span class="number">10</span><span class="special">])</span> <span class="keyword">const</span>
210 is supported provided <code class="computeroutput"><span class="identifier">MyClass</span></code>
214 The Typeof Library also provides registration files for most STL classes/templates.
215 These files are located in the std subdirectory, and named after corresponding
216 STL headers. These files are not included by the typeof system and have to
217 be explicitly included by the user, as needed:
219 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">typeof</span><span class="special">/</span><span class="identifier">std</span><span class="special">/</span><span class="identifier">functional</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
220 <span class="identifier">BOOST_AUTO</span><span class="special">(</span><span class="identifier">fun</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bind2nd</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special"><</span><span class="keyword">int</span><span class="special">>(),</span> <span class="number">21</span><span class="special">));</span> <span class="comment">//create named function object for future use.</span>
223 <div class="section">
224 <div class="titlepage"><div><div><h3 class="title">
225 <a name="typeof.what"></a>What needs to be registered?</h3></div></div></div>
227 It is possible to take advantage of the compiler when registering types for
228 the Typeof Library. Even though there is currently no direct support for
229 typeof in the language, the compiler is aware of what the type of an expression
230 is, and gives an error if it encounters an expression that has not been handled
231 correctly. In the <code class="computeroutput"><span class="identifier">typeof</span></code>
232 context, this error message will contain clues to what types needs to be
233 registered with the Typeof Library in order for <code class="computeroutput"><span class="identifier">BOOST_TYPEOF</span></code>
236 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">X</span> <span class="special">{};</span>
238 <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">A</span><span class="special">,</span><span class="keyword">bool</span> <span class="identifier">B</span><span class="special">></span>
239 <span class="keyword">struct</span> <span class="identifier">Y</span> <span class="special">{};</span>
241 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">X</span><span class="special">,</span><span class="identifier">Y</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">true</span><span class="special">></span> <span class="special">></span> <span class="identifier">a</span><span class="special">;</span>
243 <span class="identifier">BOOST_AUTO</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span><span class="identifier">b</span><span class="special">);</span>
246 We get the following error message from VC7.1
248 <pre class="programlisting">error C2504: 'boost::type_of::'anonymous-namespace'::encode_type_impl<V,Type_Not_Registered_With_Typeof_System>' : base
252 V=boost::type_of::'anonymous-namespace'::encode_type_impl<boost::mpl::vector0<boost::mpl::na>,std::pair<X,Y<int,true>>>::V0,
253 Type_Not_Registered_With_Typeof_System=X
257 Inspecting this error message, we see that the compiler complains about
258 <code class="computeroutput"><span class="identifier">X</span></code>
260 <pre class="programlisting"><span class="identifier">BOOST_TYPEOF_REGISTER_TYPE</span><span class="special">(</span><span class="identifier">X</span><span class="special">);</span> <span class="comment">//register X with the typeof system</span>
263 Recompiling, we get a new error message from VC7.1
265 <pre class="programlisting">error C2504: 'boost::type_of::'anonymous-namespace'::encode_type_impl<V,Type_Not_Registered_With_Typeof_System>' : base
269 V=boost::type_of::'anonymous-namespace'::encode_type_impl<boost::mpl::vector0<boost::mpl::na>,std::pair<X,Y<int,true>>>::V1,
270 Type_Not_Registered_With_Typeof_System=Y<int,true>
274 Inspecting this error message, we see that the compiler complains about
275 <code class="computeroutput"><span class="identifier">Y</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">true</span><span class="special">></span></code>. Since <code class="computeroutput"><span class="identifier">Y</span></code>
276 is a template, and contains integral constants, we need to take more care
279 <pre class="programlisting"><span class="identifier">BOOST_TYPEOF_REGISTER_TEMPLATE</span><span class="special">(</span><span class="identifier">Y</span><span class="special">,(</span><span class="keyword">typename</span><span class="special">)(</span><span class="keyword">bool</span><span class="special">));</span> <span class="comment">//register template class Y</span>
282 It is a good idea to look up the exact definition of <code class="computeroutput"><span class="identifier">Y</span></code>
283 when it contains integral constants. For simple template classes containing
284 only typenames, you can rely solely on the compiler error.
287 The above code now compiles.
290 This technique can be used to get an overview of which types needs to be
291 registered for a given project in order to support <code class="computeroutput"><span class="identifier">typeof</span></code>.
294 <div class="section">
295 <div class="titlepage"><div><div><h3 class="title">
296 <a name="typeof.limi"></a>Limitations</h3></div></div></div>
298 Nested template template parameters are not supported, like:
300 <pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">template</span><span class="special"><</span><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span><span class="special">></span> <span class="keyword">class</span><span class="special">></span> <span class="keyword">class</span> <span class="identifier">Tpl</span><span class="special">></span>
301 <span class="keyword">class</span> <span class="identifier">A</span><span class="special">;</span> <span class="comment">// can't register!</span>
304 Classes and templates nested inside other templates also can't be registered
305 because of the issue of nondeduced context. This limitation is most noticeable
306 with regards to standard iterators in Dinkumware STL, which are implemented
307 as nested classes. Instead, instantiations can be registered:
309 <pre class="programlisting"><span class="identifier">BOOST_TYPEOF_REGISTER_TYPE</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special"><</span><span class="keyword">int</span><span class="special">>::</span><span class="identifier">const_iterator</span><span class="special">)</span>
313 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
314 <td align="left"></td>
315 <td align="right"><div class="copyright-footer">Copyright © 2004, 2005 Arkadiy Vertleyb, Peder Holt<p>
316 Distributed under the Boost Software License, Version 1.0. (See accompanying
317 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">
318 http://www.boost.org/LICENSE_1_0.txt </a>)
323 <div class="spirit-nav">
324 <a accesskey="p" href="refe.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../typeof.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="cont.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>