为什么不能将析构函数标记为constexpr? [英] Why can't a destructor be marked constexpr?

查看:203
本文介绍了为什么不能将析构函数标记为constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,您可以将许多内容声明为 constexpr :变量,函数(包括成员函数和运算符),构造函数,以及自C ++ 1z起, if 语句 lambda表达式.但是,声明析构函数 constexpr 会导致错误:

In C++, you can declare many things as constexpr: variables, functions (including member functions and operators), constructors, and since C++1z, also if statements and lambda expressions. However, declaring a destructor constexpr results in an error:

struct X {
    constexpr ~X() = default; // error: a destructor cannot be 'constexpr'
};

我的问题:

  1. 为什么不能将析构函数标记为 constexpr ?
  2. 如果我不提供析构函数,则隐式生成的析构函数是否是 constexpr ?
  3. 如果我声明默认的析构函数(〜X()= default; ),它会自动 constexpr 吗?
  1. Why can't a destructor be marked constexpr?
  2. If I do not provide a destructor, is the implicitly generated destructor constexpr?
  3. If I declare a defaulted destructor (~X() = default;), is it automatically constexpr?

推荐答案

按照 draft basic.types#10 可能是具有cv资格的类类型:

As per the draft basic.types#10 possibly cv-qualified class type that has all of the following properties:

可能具有cv资格的类类型,具有以下所有属性:

A possibly cv-qualified class type that has all of the following properties:

(10.5.1)-它有一个琐碎的析构函数,

(10.5.1) - it has a trivial destructor,

(10.5.2)-它是闭包类型,聚合类型或在至少一个constexpr构造函数或构造函数模板(可能不是复制或移动构造函数的基类继承的

(10.5.2) - it is either a closure type, an aggregate type, or has at least one constexpr constructor or constructor template (possibly inherited from a base class) that is not a copy or move constructor,

(10.5.3)-如果它是一个联合,则其至少一个非静态数据成员属于非易失性文字类型

(10.5.3) - if it is a union, at least one of its non-static data members is of non-volatile literal type

(10.5.4)-如果不是一个联合,其所有非静态数据成员和基类都属于非易失性文字类型.

(10.5.4) - if it is not a union, all of its non-static data members and base classes are of non-volatile literal types.

问题1:为什么不能将析构函数标记为constexpr?

因为只有琐碎的析构函数才有资格使用constexpr以下是草稿

Because only trivial destructors are qualified for constexpr Following is the relevant section of the draft

如果析构函数不是用户提供的,并且满足以下条件,则它是微不足道的:

A destructor is trivial if it is not user-provided and if:

(5.4)-析构函数不是虚拟的,

(5.4) — the destructor is not virtual,

(5.5)-此类中的所有直接基类均不重要析构函数,和

(5.5) — all of the direct base classes of its class have trivial destructors, and

(5.6)-对于该类中的所有非静态数据成员,它们是类的类型(或其数组),每个此类都有一个琐碎的破坏者.

(5.6) — for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.

否则,析构函数是不平凡的.

Otherwise, the destructor is non-trivial.

问题2:如果我不提供析构函数,则隐式生成的析构函数constexpr是吗?

是的,因为隐式生成的析构函数是微不足道的类型,所以它可以用于constexpr

Yes, because implicitly generated destructor is trivial type, so it is qualified for constexpr

问题3:如果我声明了默认的析构函数(〜X()= default;),它会自动constexpr吗?

实际上,此析构函数是用户声明的并且是隐式生成的,因此可以使用constexpr.

Indeed, this destructor is user-declared and implicitly-generated and thus it is qualified for constexpr.

我找不到任何直接引用,只有琐碎的析构函数符合 constexpr 的条件,但是如果析构函数不是琐碎的,则可以肯定该类类型不是 cv限定的.所以它有点隐式,因为您无法为 cv限定的类定义 destructor .

I'm not able to find any direct reference that only trivial destructors are qualified for constexpr but if the destructor is not trivial then it is for sure that class type is not cv-qualified. So it kind of implicit as you can't define a destructor for cv-qualified class.

从C ++ 20开始,在某些情况下,用户定义的析构函数也可以是constexpr.

Since C++20, user defined destructors can also be constexpr under certain conditions.

dcl.constexpr/3 :

constexpr函数的定义应满足以下条件要求:

The definition of a constexpr function shall satisfy the following requirements:

  • 其返回类型(如果有)应为文字类型;
  • 其每个参数类型应为文字类型;
  • 它不能是协程([dcl.fct.def.协程]);
  • 如果函数是构造函数或析构函数,则其类不应具有任何虚拟基类;
  • 其功能主体不得封闭([stmt.pre])
    • goto语句
    • 标识符标签([stmt.label]),
    • 非文字类型或静态或线程变量的定义存储期限.

    这篇关于为什么不能将析构函数标记为constexpr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆