summaryrefslogtreecommitdiffstats
path: root/www/cxx_compatibility.html
diff options
context:
space:
mode:
authorJohn McCall <rjmccall@apple.com>2010-04-09 01:07:07 +0000
committerJohn McCall <rjmccall@apple.com>2010-04-09 01:07:07 +0000
commit5dd52ac75e39f24bdd47a856968ae132a82b23db (patch)
tree10bc1ad7d94d90ab48b827ce9d4a86467ab18a2a /www/cxx_compatibility.html
parentd4eea8362605807327735727a9098abe1eb23b19 (diff)
Add a note to the C++ compatibility page about templates with no
valid instantiations. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@100836 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'www/cxx_compatibility.html')
-rw-r--r--www/cxx_compatibility.html48
1 files changed, 48 insertions, 0 deletions
diff --git a/www/cxx_compatibility.html b/www/cxx_compatibility.html
index e5fd63ef9c..bd48c6c09d 100644
--- a/www/cxx_compatibility.html
+++ b/www/cxx_compatibility.html
@@ -25,6 +25,7 @@
<li><a href="#init_static_const">Initialization of non-integral static const data members within a class definition</a></li>
<li><a href="#dep_lookup">Unqualified lookup in templates</a></li>
<li><a href="#dep_lookup_bases">Unqualified lookup into dependent bases of class templates</a></li>
+<li><a href="#bad_templates">Templates with no valid instantiations</a></li>
<li><a href="#default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</a></li>
</ul>
@@ -203,6 +204,53 @@ if <tt>DoThis</tt> is virtual, calling it this way will bypass virtual
dispatch!
<!-- ======================================================================= -->
+<h2 id="bad_templates">Templates with no valid instantiations</h2>
+<!-- ======================================================================= -->
+
+The following code contains a typo: the programmer
+meant <tt>init()</tt> but wrote <tt>innit()</tt> instead.
+
+<pre>
+ template &lt;class T&gt; class Processor {
+ ...
+ void init();
+ ...
+ };
+ ...
+ template &lt;class T&gt; void process() {
+ Processor&lt;T&gt; processor;
+ processor.innit(); // <-- should be 'init()'
+ ...
+ }
+</pre>
+
+Unfortunately, we can't flag this mistake as soon as we see it: inside
+a template, we're not allowed to make assumptions about "dependent
+types" like <tt>Processor&lt;T&gt;</tt>. Suppose that later on in
+this file the programmer adds an explicit specialization
+of <tt>Processor</tt>, like so:
+
+<pre>
+ template &lt;&gt; class Processor&lt;char*&gt; {
+ void innit();
+ };
+</pre>
+
+Now the program will work &mdash; as long as the programmer only ever
+instantiates <tt>process()</tt> with <tt>T = char*</tt>! This is why
+it's hard, and sometimes impossible, to diagnose mistakes in a
+template definition before it's instantiated.
+
+<p>The standard says that a template with no valid instantiations is
+ill-formed. Clang tries to do as much checking as possible at
+definition-time instead of instantiation-time: not only does this
+produce clearer diagnostics, but it also substantially improves
+compile times when using pre-compiled headers. The downside to this
+philosophy is that Clang sometimes fails to process files because they
+contain broken templates that are no longer used. The solution is
+simple: since the code is unused, just remove it.
+
+<!-- ======================================================================= -->
<h2 id="default_init_const">Default initialization of const variable of a class type requires user-defined default constructor</h2>
<!-- ======================================================================= -->