简体   繁体   English

C++11:文字 7 是纯右值?

[英]C++11: literal 7 is a prvalue?

I came across this article .我偶然发现了这篇文章 And its example says:它的例子说:

int main()
{
    int i, j, *p;

    // Correct usage: the variable i is an lvalue and the literal 7 is a prvalue.
    i = 7;
...
}

I wonder why literal 7 is a prvalue.我想知道为什么文字 7 是纯右值。

The standard [7.2.1.1.2] says:标准 [7.2.1.1.2] 说:

A prvalue is an expression whose evaluation
initializes an object or a bit-field, or computes the value of an
operand of an operator, as specified by the context in which it
appears, or an expression that has type cv void.

The expression 7 itself doesn't initializes an object nor a bit-field, nor computes the value of an operand of an operator as 7 itself doesn't have an operator.表达式7本身不会初始化 object 或位字段,也不会计算运算符的操作数的值,因为7本身没有运算符。

Maybe I don't interpret the standard correctly.也许我没有正确解释标准。

The standard quote you used describes the properties of prvalues, but does not define them.您使用的标准引用描述了纯右值的属性,但没有定义它们。 The definitions are scattered through the standard (and gathered on cppreference ).定义分散在标准中(并收集在 cppreference 上)。

The definition for your situation is found in [expr.prim.literal]/1 :您的情况的定义可在[expr.prim.literal]/1中找到:

A string-literal is an lvalue, a user-defined-literal has the same value category as the corresponding operator call expression described in [lex.ext], and any other literal is a prvalue字符串文字是左值,用户定义文字与 [lex.ext] 中描述的相应运算符调用表达式具有相同的值类别,任何其他文字都是纯右值

The expression 7 itself doesn't initializes an object nor a bit-field表达式7本身不会初始化 object 或位字段

Actually it does, it initializes the 7 object.实际上确实如此,它初始化了7 object。 See https://en.cppreference.com/w/cpp/language/value_category请参阅https://en.cppreference.com/w/cpp/language/value_category

prvalue公允价值

The following expressions are prvalue expressions:以下表达式是纯右值表达式:

  • a literal (except for string literal), such as 42 , true or nullptr ;文字(字符串文字除外),例如42truenullptr ... ...

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

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