-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clang rejects regular constexpr member function in struct with virtual base class #97266
Comments
For the first one (normal member function) llvm-project/clang/lib/Sema/SemaDeclCXX.cpp Lines 1826 to 1827 in 1b8ab2f
In C++20 (and before), this matches all of the requirements https://timsong-cpp.github.io/cppwp/n4861/dcl.constexpr#3 Notably C++20 had a requirement for constructors/destructors only:
Which is being applied to all member functions by Clang. Stuff like this needs to be accepted: https://godbolt.org/z/76WzG3jv6 For the second one (defaulted copy-assign operator): In C++23 after p2448r2, it should be accepted. The constructors not being allowed to be
So I believe gcc shouldn't accept it before C++23. https://timsong-cpp.github.io/cppwp/n4861/dcl.fct.def.default#3.sentence-1 (C++20)
|
There is even a FIXME comment about this: llvm-project/clang/lib/Sema/SemaDeclCXX.cpp Lines 1820 to 1841 in 208a08c
|
@llvm/issue-subscribers-clang-frontend Author: None (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 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. |
However, in this example it seems that As In C++23, it should be well-formed to make |
@frederick-vs-ja You don't need an object to start its lifetime inside the constant expression to call a member function on it (https://stackoverflow.com/q/32803708) struct base { };
struct derived : virtual base {
constexpr int tst() {return 1;}
};
extern derived d;
static_assert(d.tst() == 1); // Well formed in C++20 (C++11 even I think) |
Yeah, you're right (thanks to P2280R4). Not sure whether #95474 should also address this. |
It was valid even before because there are no unknown references ( |
Wow. Looks like I got this wrong 13 years ago. Oops! |
First, the following code is rejected by clang but accepted by other compilers like GCC, MSVC, and ICC:
https://godbolt.org/z/rj63nKP4v
Clang reports the following message:
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:
https://godbolt.org/z/rYPEYTex8
Copy ctors and move ctors are always rejected though.
The text was updated successfully, but these errors were encountered: