[PATCH] D158540: Improve error message for constexpr constructors of virtual base classes

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 28 09:15:20 PDT 2023


aaron.ballman added a comment.

In D158540#4643563 <https://reviews.llvm.org/D158540#4643563>, @NoumanAmir657 wrote:

> **Code:**
>
>   struct Base {
>    constexpr Base() = default;
>   };
>   struct Derived : virtual Base {
>     constexpr Derived() = default;
>   };
>   
>   struct NoCopyMove {
>     constexpr NoCopyMove() {}
>     NoCopyMove(const NoCopyMove&);
>     NoCopyMove(NoCopyMove&&);
>   };
>   struct S2 {
>     constexpr S2() = default;
>     constexpr S2(const S2&) = default;
>     constexpr S2(S2&&) = default; 
>     NoCopyMove ncm;
>   };
>
> **Error Generated**
>
>   ../../llvm-test/x.cpp:5:3: error: default constructor cannot be 'constexpr' in a class with virtual base class
>       5 |   constexpr Derived() = default;
>         |   ^
>   ../../llvm-test/x.cpp:4:18: note: virtual base class declared here
>       4 | struct Derived : virtual Base {
>         |                  ^
>   ../../llvm-test/x.cpp:15:3: error: defaulted definition of copy constructor is not constexpr
>      15 |   constexpr S2(const S2&) = default;
>         |   ^
>   ../../llvm-test/x.cpp:16:3: error: defaulted definition of move constructor is not constexpr
>      16 |   constexpr S2(S2&&) = default; 
>         |   ^
>   3 errors generated.
>
> @aaron.ballman do you mean like this? This now generates different diagnostics depending on whether a virtual base class is present or not

Yes, this is along the lines of what I was thinking.

In D158540#4648361 <https://reviews.llvm.org/D158540#4648361>, @NoumanAmir657 wrote:

> @aaron.ballman 
> why the member is not an constexpr can be seen from getNumVBases(). The 'defaultedSpecialMemberIsConstexpr'  returns false whenever getVNumBases is true for this so we can use that to identify when to give which error diagnostic.
> Can you verify whether this would work or is there any problem with this. I haven't uploaded the diff yet

That sounds like a reasonable direction to me!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158540/new/

https://reviews.llvm.org/D158540



More information about the cfe-commits mailing list