什么是具有静态存储持续时间的临时对象 [英] What is a temporary object with static storage duration

查看:256
本文介绍了什么是具有静态存储持续时间的临时对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

灵感来自此答案,来自

常量表达式可以是表示实体的glvalue核心常量表达式,该实体是常量表达式(如下定义)的允许结果,也可以是其值满足以下约束的prvalue核心常量表达式:

A constant expression is either a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints:

  • 如果该值是类类型的对象,则引用类型的每个非静态数据成员都引用一个实体,该实体是常量表达式的允许结果,

  • if the value is an object of class type, each non-static data member of reference type refers to an entity that is a permitted result of a constant expression,

如果该值是指针类型,则它包含具有静态存储持续时间的对象的地址,该对象末尾的地址([expr.add]),函数的地址或空指针值,以及

if the value is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object ([expr.add]), the address of a function, or a null pointer value, and

如果该值是类或数组类型的对象,则每个子对象都满足该值的这些约束.

if the value is an object of class or array type, each subobject satisfies these constraints for the value.

如果实体是具有静态存储持续时间的对象,该对象要么不是临时对象,要么是其值满足上述约束的临时对象,则它是常量表达式的允许结果,或者是一个功能.

An entity is a permitted result of a constant expression if it is an object with static storage duration that is either not a temporary object or is a temporary object whose value satisfies the above constraints, or it is a function.

具有静态存储持续时间的临时对象到底是什么?我是否缺少某些东西,或者某个对象既是临时对象又具有静态存储期限是自相矛盾的?

What exactly is a temporary object with static storage duration? Am I missing something or is it paradoxical for an object to both be temporary and have static storage duration?

[basic.stc.static] 中的定义

所有没有动态存储持续时间,没有线程存储持续时间以及非本地变量的变量都具有静态存储持续时间.这些实体的存储应在程序执行期间保持

All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program

仅适用于变量.

推荐答案

[basic.stc]/1告诉我们:

[basic.stc]/1 tells us:

存储持续时间是对象的属性,它定义了包含该对象的存储的最小潜在寿命.

The storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object.

因此,每个对象都有一个存储期限.此外,第2段说:

So every object has a storage duration. Further, paragraph 2 says:

静态,线程和自动存储的持续时间与声明(6.1)引入的对象以及实现(15.2)隐式创建的对象相关联.

已添加重点.请注意,第15.2节是[class.temporary]:临时对象的规则.

Emphasis added. Note that section 15.2 is [class.temporary]: the rules for temporary objects.

因此,我们可以得出结论,临时对象具有存储期限.我们可以得出结论,临时人员必须具有这些存储时间之一.确实,标准中有很多关于变量或临时对象"及其存储持续时间的引用.

Therefore, we can conclude that temporary objects have storage durations. And we can conclude that temporaries must have one of those storage durations. Indeed, there are numerous references in the standard to "variables or temporary objects" and their storage durations.

尽管如此,尽管清楚地表明临时对象具有这些存储期限之一,但是该标准从未实际说出它们具有什么存储期限. [class.temporary]没有声明说绑定到引用的临时对象具有其引用的存储期限. [basic.stc]对静态,自动和线程本地持续时间的解释总会涉及变量.

However, despite this clearly saying that temporary objects have one of those storage durations... the standard never actually says what storage duration they have. [class.temporary] does not have a statement saying that temporaries bound to references have the storage duration of their references. And [basic.stc]'s explanation of static, automatic, and thread-local durations always speaks of variables.

所以我要说这是措辞上的缺陷.显然,标准的期望临时对象具有适当的存储期限.标准在多个地方讨论了可变或临时对象的存储持续时间.但是它从不说出他们实际拥有的存储时间.

So I would say that this is a defect in the wording. It seems clear that the standard expects temporaries to have an appropriate storage duration; there are multiple places where the standard talks about the storage duration of variable or temporary objects. But it never says what storage duration they actually have.

这篇关于什么是具有静态存储持续时间的临时对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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