PR值的简历资格(重审) [英] Cv-qualifications of prvalues (revisited)

查看:122
本文介绍了PR值的简历资格(重审)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的上一个问题的后续行动,在这里,明显的共识是,用于解决prvalue的cv限定词的更改只是一个很小的,无关紧要的更改,目的是解决某些不一致情况(例如,返回prvalue并使用cv限定的返回类型声明的函数).

This is a followup to my previous question, where the apparent consensus was that the change in treatment of cv-qualifications of prvalues was just a fairly minor and inconsequential change intended to solve some inconsistencies (e.g. functions returning prvalues and declared with cv-qualified return types).

但是,我看到标准中的另一个地方似乎依赖具有cv限定类型的prvalue:通过临时物化转换用prvalue初始化const引用.可以在 9.3.3/5

However, I see another place in the standard that appears to rely on prvalues having cv-qualified types: initialization of const references with prvalues through temporary materialization conversion. The relevant wording can be found in multiple spots in 9.3.3/5

[...]如果转换后的初始值设定项是prvalue,则将其类型T4调整为类型"cv1 T4"([conv.qual]),并应用临时实现转换([conv.rval]). ..]

[...] If the converted initializer is a prvalue, its type T4 is adjusted to type "cv1 T4" ([conv.qual]) and the temporary materialization conversion ([conv.rval]) is applied [...]

[...]否则,初始化程序表达式将隐式转换为类型为"cv1 T1"的prvalue.应用临时物化转换,并将引用绑定到结果.

[...] Otherwise, the initializer expression is implicitly converted to a prvalue of type "cv1 T1". The temporary materialization conversion is applied and the reference is bound to the result.

显然,其目的是确保当我们到达实际的临时物化转换时

The intent is obviously to make sure that when we get to the actual temporary materialization conversion

7.3.4临时实现转换
1 可以将类型T的prvalue转换为类型T的xvalue.此转换通过使用临时对象评估prvalue来从prvalue初始化类型T的临时对象([class.temporary]).作为其结果对象,并产生一个表示临时对象的xvalue. [...]

7.3.4 Temporary materialization conversion
1 A prvalue of type T can be converted to an xvalue of type T. This conversion initializes a temporary object ([class.temporary]) of type T from the prvalue by evaluating the prvalue with the temporary object as its result object, and produces an xvalue denoting the temporary object. [...]

它接收作为输入的类型T包括所需的cv资格.

the type T that it receives as input includes the required cv-qualifications.

但是,这种cv资格如何在 7.2.2/2 如果是非类非数组prvalue?

But how does that cv-qualification survive the 7.2.2/2 in case of non-class non-array prvalue?

7.2.2类型
2 如果prvalue最初的类型为"cv T",其中T是cv不合格的非类,非数组类型,则在进行任何进一步分析之前,将表达式的类型调整为T

7.2.2 Type
2 If a prvalue initially has the type "cv T", where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.

是吗?

例如在这个例子中我们得到什么样的临时结果

E.g. what kind of temporary do we get in this example

const int &r = 42;

是临时const吗?我们可以做

const_cast<int &>(r) = 101; // Undefined or not?

是否触发未定义的行为?如果我没记错的话,最初的目的是在这种情况下获得一个const int临时目录.还是这样吗? (对于类类型,答案很明确-我们得到一个const临时变量.)

without triggering undefined behavior? If I'm not mistaken, the original intent was to obtain a const int temporary in such cases. Is it still true? (For class types the answer is clear - we get a const temporary.)

推荐答案

对于

const int& r1=42

引用的类型为const int,因此 cv1 const,而prvalue的类型为const int,因此物化临时类型也为const int.

The reference is of type const int, so cv1 is const, and the prvalue is of type const int, so the materialized temporary is also of type const int.

这篇关于PR值的简历资格(重审)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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