【发布时间】:2019-10-23 03:58:57
【问题描述】:
以下代码在 gcc 9.1 godbolt 中编译,但不是 clang 8 godbolt:
class A {
protected:
~A() = default;
};
class B final : public A {
};
int main() {
auto b = B{};
}
Clang 的错误:
<source>:10:16: error: temporary of type 'A' has protected destructor
auto b = B{};
^
<source>:3:5: note: declared protected here
~A() = default;
^
哪个是正确的,为什么?
【问题讨论】:
-
(略)具有相同行为的更简单版本:godbolt.org/z/VUBXqd 基于此,我倾向于认为这是一个错误,因为没有显式默认初始化的相同内容可以编译。 (
B b{}失败,而B b成功) -
final重要吗?编辑:看来不是 -
Clang 应该是正确的。这是由于聚合。试试
auto b =B();看看它是否可以编译。是stackoverflow.com/questions/56367480/…的副本
标签: c++ language-lawyer c++17 destructor compiler-generated