Skip to content

A constexpr virtual function rejects valid usage of CRTP type as "incomplete type" - compiles with gccΒ #113801

@kirshamir

Description

@kirshamir

C++20 allows constexpr virtual functions.

The following code 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationdiverges-from:edgDoes the clang frontend diverge from edg compiler on this issuediverges-from:gccDoes the clang frontend diverge from gcc on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions