[llvm-bugs] [Bug 34728] New: PCH not preserving defaults for template parameters

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Sep 25 19:55:58 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34728

            Bug ID: 34728
           Summary: PCH not preserving defaults for template parameters
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: steve at obrien.cc
                CC: llvm-bugs at lists.llvm.org

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); }

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170926/ce8b7430/attachment.html>


More information about the llvm-bugs mailing list