From: John McCall Date: Fri, 9 Apr 2010 01:07:07 +0000 (+0000) Subject: Add a note to the C++ compatibility page about templates with no X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5dd52ac75e39f24bdd47a856968ae132a82b23db;p=clang 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 --- 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 @@
  • Initialization of non-integral static const data members within a class definition
  • Unqualified lookup in templates
  • Unqualified lookup into dependent bases of class templates
  • +
  • Templates with no valid instantiations
  • Default initialization of const variable of a class type requires user-defined default constructor
  • @@ -202,6 +203,53 @@ This works whether the methods are static or not, but be careful: if DoThis is virtual, calling it this way will bypass virtual dispatch! + +

    Templates with no valid instantiations

    + + +The following code contains a typo: the programmer +meant init() but wrote innit() instead. + +
    +  template <class T> class Processor {
    +    ...
    +    void init();
    +    ...
    +  };
    +  ...
    +  template <class T> void process() {
    +    Processor<T> processor;
    +    processor.innit();       // <-- should be 'init()'
    +    ...
    +  }
    +
    + +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 Processor<T>. Suppose that later on in +this file the programmer adds an explicit specialization +of Processor, like so: + +
    +  template <> class Processor<char*> {
    +    void innit();
    +  };
    +
    + +Now the program will work — as long as the programmer only ever +instantiates process() with T = char*! This is why +it's hard, and sometimes impossible, to diagnose mistakes in a +template definition before it's instantiated. + +

    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. +

    Default initialization of const variable of a class type requires user-defined default constructor