[llvm-bugs] [Bug 48365] New: Compiler is using wrong template argument when a child class calls a templated constructor of a template class.

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Dec 2 14:25:24 PST 2020


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

            Bug ID: 48365
           Summary: Compiler is using wrong template argument when a child
                    class calls a templated constructor of a template
                    class.
           Product: libc++
           Version: 11.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: nicolas.brun at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

The following code doesn't compile correctly:

<<
#include <type_traits>

template <typename Type>
struct CheckInt {
  using success = std::bool_constant<std::is_same_v<Type, float>>;
  static_assert(success());
};

template <typename T>
class Base {
  public:
    template <typename U, typename = typename CheckInt<U>::success>
    Base(const U& v) {}
};

class Child : public Base<int> {
  public:
    Child() : Base(1.0f) {}
};

int main()
{
  Child child; // doesn't compile
  //Base<int> base(1.0f); // compiles

  return 0;
}
>>

When instantiating Child, the static_assert() fires because U evaluates to
`Base<int>` instead of `float`.
When instantiation Base<int> directly, U evaluates  to `float` correctly.

The compiler generates the following error:
"
<source>:6:3: error: static_assert failed due to requirement
'std::integral_constant<bool, false>()'
  static_assert(success());
  ^             ~~~~~~~~~
<source>:12:47: note: in instantiation of template class 'CheckInt<Base<int>>'
requested here
    template <typename U, typename = typename CheckInt<U>::success>
"

The bug appears in all versions of clang (as well as gcc).

See https://godbolt.org/z/73c4Wd

-- 
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/20201202/a40a506b/attachment.html>


More information about the llvm-bugs mailing list