<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/113801>113801</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
A constexpr virtual function rejects valid usage of CRTP type as "incomplete type" - compiles with gcc
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kirshamir
</td>
</tr>
</table>
<pre>
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.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVkGT2jgT_TXi0jWULQ0YDj4YCJf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