简体   繁体   English

一元*运算符的操作数是否期望纯右值

[英]Does the operand of unary* operator expect a prvalue

int main(){
  int a = 0;
  auto ptr = &a;
  auto&& rf = *ptr;
}

Consider the above code, when glvalue ptr is used to as the operand of unary* operator, Is it be required a lvalue-to-rvalue conversion that would applied for it?考虑上面的代码,当使用 glvalue ptr作为 unary* 运算符的操作数时,是否需要应用它的左值到右值转换? IE, Does the operand of unary* operator expect a prvalue? IE,一元*运算符的操作数是否需要纯右值?

Question 1:问题一:

expr.unary.op#1 expr.unary.op#1

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T”, the type of the result is “T”.如果表达式的类型是“指向 T 的指针”,则结果的类型是“T”。 [ Note: Indirection through a pointer to an incomplete type (other than cv void) is valid. [ 注意:通过指向不完整类型(除了 cv void)的指针间接是有效的。 The lvalue thus obtained can be used in limited ways (to initialize a reference, for example);这样获得的左值可以以有限的方式使用(例如,初始化引用); this lvalue must not be converted to a prvalue, see [conv.lval].此左值不得转换为纯右值,请参阅 [conv.lval]。 — end note ] ——尾注]

What kind of value categories does operands of unary* operator expect.一元*运算符的操作数期望什么样的值类别。


Beside this operator, It seems to the most operators in [expr] is not specified what kind of value categories its operands expect.除了这个运算符,在 [expr] 中的大多数运算符似乎都没有指定它的操作数期望什么样的值类别。

expr#9表达式#9

Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue, array-to-pointer, or function-to-pointer standard conversions are applied to convert the expression to a prvalue.每当一个泛左值表达式作为一个运算符的操作数出现时,该运算符需要一个纯右值,左值到右值、数组到指针或函数到指针的标准转换将应用于将表达式转换为纯右值。 [ Note: Because cv-qualifiers are removed from the type of an expression of non-class type when the expression is converted to a prvalue, an lvalue expression of type const int can, for example, be used where a prvalue expression of type int is required. [ 注意:因为当表达式转换为纯右值时,非类类型表达式的类型中会删除 cv 限定符,所以可以使用 const int 类型的左值表达式,例如,用于 int 类型的纯右值表达式是必须的。 — end note ]. ——尾注]。

The quote above is so unclear.上面的引用太不清楚了。

Question 2:问题2:

As aforementioned, the most operators in [expr] is not specified what kind of value categories its operands expect.如前所述,[expr] 中的大多数运算符都没有指定其操作数期望的值类别。 So It it a defect?所以这是一个缺陷吗?

In general, it is CWG1642 :一般来说,它是CWG1642

1642. Missing requirements for prvalue operands 1642. prvalue 操作数的缺失要求

Although the note in 6.10 [basic.lval] paragraph 1 states that尽管 6.10 [basic.lval] 第 1 段中的注释指出

The discussion of each built-in operator in Clause 8 [expr] indicates the category of the value it yields and the value categories of the operands it expects第 8 章 [expr] 中对每个内置运算符的讨论表明了它产生的值的类别以及它期望的操作数的值类别

in fact, many of the operators that take prvalue operands do not make that requirement explicit.事实上,许多采用纯右值操作数的运算符并没有明确说明这一要求。 Possible approaches to address this failure could be a blanket statement that an operand whose value category is not stated is assumed to be a prvalue;解决此故障的可能方法可能是一揽子声明,即假定未说明值类别的操作数是纯右值; adding prvalue requirements to each operand description for which it is missing;为缺少的每个操作数描述添加纯右值要求; or changing the description of the usual arithmetic conversions to state that they imply the lvalue-to-rvalue conversion, which would cover the majority of the omissions.或将常用算术转换的描述更改为 state,它们暗示左值到右值的转换,这将涵盖大部分遗漏。

In particular, [expr.unary.op]/1 wording should be fixed by this PR .特别是,[expr.unary.op]/1 的措辞应该由这个 PR修正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM