gpt4 book ai didi

表达式和变量的 C++ 类型和值类别

转载 作者:可可西里 更新时间:2023-11-01 18:39:55 26 4
gpt4 key购买 nike

从这里link , 它说

Objects, references, functions including function template specializations, and expressions have a property called type

因此给出以下内容:

int &&rf_int = 10;

我可以说变量 rf_int 是复合类型对 int 的右值引用

但是在谈论value category时, 它特别指出

Each expression has some non-reference type

Each C++ expression (an operator with its operands, a literal, a variable name, etc.)

根据以上两条语句,rf_int可以看作是一个表达式,表达式具有非引用类型

现在我真的很困惑。 rf_int 是否有reference 类型?在谈论名称的类型(无论是变量还是表达式)时,我们是否必须提供上下文?

更具体地说,当在函数调用中使用变量名时:

SomeFunc(rf_int);

rf_int 现在是一个表达式(因此它是一个 int 类型的左值),还是一个变量(因此它是一个右值类型的左值引用 int)?

编辑:评论here让我想知道这个问题。

最佳答案

Does rf_int have a reference type or not?

名称为rf_int实体(或变量)的类型为int&&(引用类型),因为它的声明方式,但是 expression rf_int 的类型为 int(非引用类型),每 [expr]/5 :

If an expression initially has the type “reference to T” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression. [ Note: Before the lifetime of the reference has started or after it has ended, the behavior is undefined (see [basic.life]).  — end note ]


Do we have to provide context when talking about the type of a name, be it a variable or an expression?

是的,我们有。 rf_int 可以说具有不同的类型,这取决于它是指实体还是表达式。


More specifically, when a variable name is used in function call:

SomeFunc(rf_int);

Is rf_int now considered an expression (thus it is an lvalue with type int), or a variable (thus it is an lvalue with type rvalue reference to int)?

它被认为是一个表达式,它是 int 类型的左值。 (请注意,值类别是表达式的属性。说变量是左值是不正确的。)

关于表达式和变量的 C++ 类型和值类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36349360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com