Issue 113801
Summary A constexpr virtual function rejects valid usage of CRTP type as "incomplete type" - compiles with gcc
Labels new issue
Assignees
Reporter kirshamir
    C++20 allows constexpr virtual functions.

The [following code](https://godbolt.org/z/T1TfxjhPK) fails to compile with clang but passes with gcc.
Turning the function `foo` to be non-virtual or removing the constexpr make it compile. But it should be valid also as virtual constexpr, and the type A shall be complete by the time the template argument is being used.

```
class Base {
 constexpr virtual void foo(const Base& b) const {}
};

template<typename ActualType, typename Base>
class CRTP: public Base {
    constexpr void foo(const Base& b) const override {
 // assume we know here for sure that b is of type const ActualType& 
 const auto& other_s = static_cast<const ActualType&>(b);
        // dynamic_cast also fails to compile in this case
        const auto& other_d = dynamic_cast<const ActualType&>(b);
        // do something with other
    }
};

class A: public CRTP<A, Base> { };

int main() {
    constexpr A a1;
}

```

Compilation error:

```
<source>:11:31: error: no viable conversion from 'const Base' to incomplete type 'const A'
   11 |         const auto& other_s = static_cast<const ActualType&>(b);
      |                               ^ ~
<source>:18:17: note: in instantiation of member function 'CRTP<A, Base>::foo' requested here
   18 | class A: public CRTP<A, Base> {
      |                 ^
<source>:18:17: note: in instantiation of template class 'CRTP<A, Base>' requested here
<source>:18:7: note: definition of 'A' is not complete until the closing '}'
   18 | class A: public CRTP<A, Base> {
      | ^
<source>:13:31: error: 'const A' is an incomplete type
   13 | const auto& other_d = dynamic_cast<const ActualType&>(b);
      | ^           ~~~~~~~~~~~~~~~~~~~
<source>:18:7: note: definition of 'A' is not complete until the closing '}'
   18 | class A: public CRTP<A, Base> {
      |       ^
2 errors generated.
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to