<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - PCH not preserving defaults for template parameters"
   href="https://bugs.llvm.org/show_bug.cgi?id=34728">34728</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>PCH not preserving defaults for template parameters
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>steve@obrien.cc
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Precompiling this code:

  template <int foo=0, class T>
  int func(T const&);

  template <int foo, class T>
  int func(T const&) {
    return foo;
  }

then using the precompiled header with `-include-pch`, with this main .cpp:

  int main(int, char**) { return func(3.14); }

results in an error:

  main.cpp:1:32: error: no matching function for call to 'func'
  int main(int, char**) { return func(3.14); }
                                 ^~~~
  /Users/steveo/pchfix/func.h:7:7: note: candidate template ignored: couldn't
infer template argument 'foo'
    int func(T const&) {
        ^

It seems that this breaks when...
(1) there's a template function with its first arg which is defaulted, other
args after it, with no default;
(2) when the declaration (with the default, non-type value is separate),
followed by the body of the function, as it is in this example;
(3) when precompiled + used later on.

I'm not completely sure this covers all the needed conditions.

These examples somehow don't break:

  // works: two defaulted non-type params
  template <int foo=0, int bar=1>  int func(double);
  template <int foo, int bar>      int func(double) { return foo; }

  // works: two defaulted type params
  template <class A=int, class T=double>  int func(T);
  template <class A, class T>             int func(T) { return A(); }

  // works: two type params, either kind, with defaults
  template <int foo=0, class T=double>  int func(T);
  template <int foo, class T>           int func(T) { return foo; }

  // works: all-in-one decl+defn
  template <class A=int, class T>  int func(T) { return A(); }

  // works: one default arg, all-in-one
  template <class A=int>  int func(double) { return A(); }

  // works: one default arg, separate decl+defn
  template <class A=int>  int func(double);
  template <class A>      int func(double) { return A(); }

  // works: no precompiling, everything in same main TU
  template <int foo=0, class T>  int func(T const&);
  template <int foo, class T>    int func(T const&) { return foo; }
  int main(int, char**) { return func(3.14159); }</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>