Skip to content

clang rejects regular constexpr member function in struct with virtual base class #97266

Open
@Rush10233

Description

@Rush10233

First, the following code is rejected by clang but accepted by other compilers like GCC, MSVC, and ICC:

    struct base { };
    struct derived : virtual base {
        constexpr int tst(){return 1;}
    };

https://godbolt.org/z/rj63nKP4v

Clang reports the following message:

<source>:3:23: error: constexpr member function not allowed in struct with virtual base class
    3 |         constexpr int tst(){return 1;}
      |                       ^
<source>:2:22: note: virtual base class declared here
    2 |     struct derived : virtual base {
      |                      ^~~~~~~~~~~~
1 error generated.

The diagnostic seems not such necessary because member function tst doesn't involve any override and can be specified during compile stage.

It's more interesting that the overloaded copy assignment operator is also accepted by other compilers, which definitely contains the instantiation of the virtual base class:

    struct base { };
    struct derived : virtual base {
        //reject
        //constexpr derived(const derived &)=default;
        //reject
        //constexpr derived(derived &&)=default;
        //accept
        constexpr derived& operator=(derived const&) =default;
    };

https://godbolt.org/z/rYPEYTex8

Copy ctors and move ctors are always rejected though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions