The following code compiles in gcc 9.1 godbolt but not clang 8 godbolt:
class A {
protected:
~A() = default;
};
class B final : public A {
};
int main() {
auto b = B{};
}
Clang's error:
<source>:10:16: error: temporary of type 'A' has protected destructor
auto b = B{};
^
<source>:3:5: note: declared protected here
~A() = default;
^
Which is correct and why?
Yes, Clang is correct in rejecting the code.
In
auto b = B{};we have an aggregate initialization, which happens directly inmainfunction. So this function must be able to call destructors ofBsubtypes in case of exception occurrence during the initialization.A quotation from N4861 (the last C++20 draft), [dcl.init.aggr]/8:
Just to be complete, a quote from [class.dtor]/15: