Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/mpl/doc/tutorial/eti.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 7.8 KB
Line 
1<?xml version="1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" />
7<title>THE BOOST MPL LIBRARY: ETI</title>
8<link rel="stylesheet" href="../style.css" type="text/css" />
9</head>
10<body class="docframe">
11<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./incomplete-support-for.html" class="navigation-link">Prev</a>&nbsp;<a href="./resources.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./incomplete-support-for.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./portability.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
12<td class="header-group page-location"><a href="../index.html" class="navigation-link">Front Page</a> / <a href="./technical-details.html" class="navigation-link">Technical Details</a> / <a href="./portability.html" class="navigation-link">Portability</a> / <a href="./eti.html" class="navigation-link">ETI</a></td>
13</tr></table><div class="header-separator"></div>
14<div class="section" id="eti">
15<h1><a class="toc-backref" href="./portability.html#id76" name="eti">ETI</a></h1>
16<p>In context of C++ template problems, ETI is an abbreviation for &quot;Early
17Template Instantiation&quot; — a Microsoft Visual C++ - specific issue that
18has been a barrier to any serious work with templates on this platform until
19Microsoft developers fixed it in Visual C++ 7.1 (2003 .NET). Although the
20problem is relatively easy to work around if the right techniques
21are applied systematically through the codebase, the approach is definitely
22tedious and time-consuming. So, if one day you discover that you are spending
23too much time dealing with the issue, consider upgrading to the
24newer version of the compiler. In fact, seriously consider it regardless.
25The benefits of saved time, money and frustration are well worth the price.</p>
26<div class="section" id="eti-the-problem">
27<h2><a name="eti-the-problem">The Problem</a></h2>
28<p>Here is a short demonstration of the issue with MSVC 6.x:</p>
29<pre class="literal-block">
30template&lt; typename F, typename T &gt; struct apply1
31{
32    typedef typename F::template apply&lt;T&gt;::type type;
33};
34</pre>
35<p>Trying to compiling this innocent-looking code, we get:</p>
36<pre class="literal-block">
37portability.cpp(4) : error C2903: 'apply' : symbol is neither a class template
38            nor a function template
39        portability.cpp(5) : see reference to class template instantiation
40            'apply1&lt;F,T&gt;' being compiled
41portability.cpp(4) : error C2143: syntax error : missing ',' before '&lt;'
42        portability.cpp(5) : see reference to class template instantiation
43            'apply1&lt;F,T&gt;' being compiled
44portability.cpp(4) : error C2059: syntax error : '&lt;'
45        portability.cpp(5) : see reference to class template instantiation
46            'apply1&lt;F,T&gt;' being compiled
47</pre>
48<p>The &quot;symbol is neither a class template nor a function template&quot; part of the
49diagnostics is actually often an indication of ETI-related problems. Another
50typical error message usually says something about nested type such-and-such
51not being a member of a global namespace.</p>
52<p>Both cases are two sides of the same compiler bug, which we call
53&quot;Early template instantiation&quot;: the compiler, for internal
54purposes, in order to process class template definitions,
55instantiates class templates with dummy template parameters
56(<tt class="literal"><span class="pre">int</span></tt>'s). That can happen both during parsing of template
57definitions (and such errors are most easy to identify and fix —
58the template definition itself just doesn't compile; the example
59above falls into this category), or later during template
60instantiation, and these one are hard to detect — the bug will
61only be triggered in some particular context.</p>
62<!-- namespace-scope: nested templates are immune? -->
63<p>ETI is always performed during parsing of the namespace-scope
64template definition, which basically means that any template
65definition that is rendered invalid by substituting its template
66parameters by <tt class="literal"><span class="pre">int</span></tt>s might not compile, as it happened with our
67example:</p>
68<pre class="literal-block">
69template&lt; typename F, typename T &gt; struct apply1
70{
71    // typedef typename F::template apply&lt;T&gt;::type type;
72    // ETI generates this:
73    typedef typename int::template apply&lt;int&gt;::type type;
74};
75</pre>
76<p>If you compile this, you'll get <em>exactly</em> the same diagnostics as we've just seen.</p>
77<p>Note that we've said &quot;might not compile&quot;, because... well, the short answer is,
78&quot;it depends&quot;. We haven't analyzed things to the point that we could tell you the
79exact condition when ETI leads to an error and when it doesn't, but
80that's not very important anyway — if it's an error, you just fix it (we'll show you how
81in a second), and if it's not, then you leave things as is. If one day the
82potential issue turns into a real one, then you apply the workaround we are about
83to give you.</p>
84</div>
85<div class="section" id="eti-the-symptoms">
86<h2><a name="eti-the-symptoms">The Symptoms</a></h2>
87<p>We've already looked at the typical diagnostics, so we won't repeat
88ourselves.  Instead we'll
89just mention that many MSVC's INTERNAL COMPILER ERRORs (ICEs) are, in fact, caused
90by an ETI-related problem somewhere deep down the instantiation stack.</p>
91</div>
92<div class="section" id="eti-the-solution">
93<h2><a name="eti-the-solution">The Solution</a></h2>
94<p>There is no way we can change the compiler's behavior in this case, so what we have
95to do is to adjust to it and still make our templates do what we want. Surprisingly,
96in most cases it's quite simple to achieve:</p>
97<pre class="literal-block">
98// potentially unsafe
99template&lt; typename F &gt; struct apply0
100{
101    typedef typename F::type type;
102};
103
104// now ETI-safe
105template&lt;&gt; struct apply0&lt;int&gt;
106{
107    typedef int type;
108};
109</pre>
110<p>Since the original template could have never been instantiated with <tt class="literal"><span class="pre">int</span></tt>,
111providing a stub <tt class="literal"><span class="pre">int</span></tt> specialization is completely innocent.</p>
112<!-- Looks like you're missing lots of stuff, like ETI_BASE - - no? -->
113</div>
114</div>
115
116<div class="footer-separator"></div>
117<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./incomplete-support-for.html" class="navigation-link">Prev</a>&nbsp;<a href="./resources.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./incomplete-support-for.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./portability.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
118</tr></table></body>
119</html>
Note: See TracBrowser for help on using the repository browser.