考虑以下代码:
#include <iostream>
class Test
{
public:
constexpr Test(const int x) : _x(x) {}
constexpr int get() const {return _x;}
~Test() {} // HERE
protected:
const int _x;
};
int main()
{
static constexpr Test test(5);
return 0;
}
如果我删除HERE
这一行,代码就可以编译通过,但是如果我定义一个空析构函数,就会产生编译错误,说Test
是非字面的。
为什么会这样,并且空析构函数和没有析构函数有什么区别?
编辑:另一个相关问题:如果空的字面值析构函数与非空析构函数不同,如何定义一个受保护的字面值析构函数?
~Test() = default;
。 - Xeoprotected
,而不是析构函数呢? - Konrad Rudolph