RValues 由什么构成?

发布于 2024-12-22 03:45:13 字数 239 浏览 1 评论 0原文

RValue 是不可操作的内存区域,因此像整数这样的文字被视为 RValue。

  1. 常数构成 RValue 吗? const int x = 0; 至少可操作一次。
  2. 现在,编译器创建的临时对象也是 RValue,即使它们具有可操作的内存区域。为什么会这样?
    因为它们不能被“用户”修改?是这个原因吗?

那么,“用户”不可操作的内存区域称为 RValue 吗?

RValues are things which are not maniputable regions of memory, so literals like integers are considered RValues.

  1. Do constants constitute RValues? const int x = 0; is maniputable at least one time.
  2. Now, the temporary objects created by the compiler are also RValues even when they have maniputable memory regions. Why is that so?
    Because they cannot be modified by "users"? Is this the reason?

So, a memory region which is NOT maniputable by the "users" is called RValue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

霓裳挽歌倾城醉 2024-12-29 03:45:14

首先 - lval 和 rval 是表达式的属性 - 表达式可以是 rval 或 lval。

要回答您的另一个问题,可以通过 rval 引用来修改由 rvals 创建的临时 - 此功能是在 c++11 中添加的此功能

对于 1) 应用移动语义 2) 完美转发非常有用。

有关详细信息,请访问 http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx

first of all - lval and rval are properties of expressions - an expression is either an rval or an lval.

To answer your other question, temporaries created by rvals can be modified by having a rval reference - this feature is added in c++11

This feature is very useful for 1) applying move semantics 2 ) perfect forwarding.

Read more about it at http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx

漫漫岁月 2024-12-29 03:45:14

RValue 是不可操作的内存区域

。 错误。右值绝对是可变的。在 C++03 中这样做是一件令人头疼的事情,但可以合法地完成 - 在 C++11 中这是非常常见的。

右值是产生即将消亡的东西的表达式。这是他们的定义属性。因此,您可以使用 C++11 的右值引用来窃取他们的资源而无需担心,或者如果您绝望的话,可以在 C++03 中“交换”。这使得它们在许多情况下更加正确,并且在很多很多情况下更快。

RValues are things which are not maniputable regions of memory

Wrong. Rvalues absolutely are mutable. In C++03 doing this is a headache, but can be done legally- in C++11 it's extremely common.

Rvalues are expressions that yield things which are about to die. That's their defining property. Therefore, you can steal their resources without worrying about it, using C++11's rvalue references, or if you're desperate, "swaptimizing" in C++03. This makes them more correct in many situations, and much faster in many, many circumstances.

半窗疏影 2024-12-29 03:45:13

标量右值是计算为标量值的表达式,如 42i + k (而标量左值是计算为标量对象的表达式,如 i*int_ptrnumbers[0])。

类类型的右值是计算结果为临时对象的表达式。最突出的例子是调用按值返回类对象的函数:

std::string foo()
{
    return "hello world";
}

给定上述函数定义,表达式 foo() 是一个右值。请注意,我不是在谈论结果(这是一个临时对象),而是在谈论表达式 foo(),其求值会产生该结果。

Scalar rvalues are expressions that evaluate to scalar values like 42 or i + k (whereas scalar lvalues are expressions that evaluate to scalar objects like i or *int_ptr or numbers[0]).

rvalues of class type are expressions that evaluate to temporary objects. The most prominent example is the call of a function that returns a class object by value:

std::string foo()
{
    return "hello world";
}

Given the above function definition, the expression foo() is an rvalue. Note that I'm not talking about the result (which is a temporary object) but the expression foo() whose evaluation yields that result.

纵情客 2024-12-29 03:45:13
  1. 常量构成 RValue 吗?常量 int x = 0;可操纵于
    至少一次。

在您的声明中,x 既不是右值也不是左值,它称为declarator-id。 (参见 C++03 中的语法 8/4)当它在(子)表达式中使用时,它是一个不可修改的左值,可以初始化为任何常量表达式

2.现在,编译器创建的临时对象也是 RValue,即使它们具有可操作的内存区域。为什么会这样呢?
因为它们不能被“用户”修改?是这个原因吗?

类类型的右值可以是可修改的,也可以是不可修改的,但内置类型的右值始终是 cv 未限定的。

§ 3.10/9 类右值可以具有 cv 限定的类型;非类右值
总是有简历不合格的类型。

考虑这个例子:

struct S {
     int x;
     void f(int s) { x = s; }
};

// ill-formed: Cannot assign to an rvalue of built-in type.
S().x = 42;

// Valid: A member function can be used to modify the referent of rvalue.
S().f(42);

子表达式 S() 创建一个类类型的右值,其生命周期是 ; 的末尾。 完整表达。

  1. Do constants constitute RValues? const int x = 0; is maniputable at
    least one time.

In your declaration, x is neither an rvalue or lvalue, it is called a declarator-id. (See grammar of 8/4 in C++03) When it is used in a (sub-)expression it is a non-modifiable lvalue which can be initialized to any constant expression.

2.Now, the temporary objects created by the compiler are also RValues even when they have maniputable memory regions. Why is that so?
Because they cannot be modified by "users"? Is this the reason?

Rvalue of a class type can either be modifiable or non-modifiable but rvalues of built-in types are always cv-unqualified.

§ 3.10/9 Class rvalues can have cv-qualified types; non-class rvalues
always have cv-unqualified types.

Consider this example:

struct S {
     int x;
     void f(int s) { x = s; }
};

// ill-formed: Cannot assign to an rvalue of built-in type.
S().x = 42;

// Valid: A member function can be used to modify the referent of rvalue.
S().f(42);

The sub-expression S() creates an rvalue of class type whose lifetime is the end of ; full-expression.

心凉怎暖 2024-12-29 03:45:13

lvalue 指的是内存位置,我们可以使用 & 运算符获取该内存位置的地址。 右值是一个不是左值的表达式。

<代码>1。常数构成 RValue 吗?常量 int x = 0;至少可操作一次。

不,因为你这样做 - const int *p = &x

An lvalue refers to a memory location and we can take the address of that memory location using the & operator. An rvalue is an expression which is not an lvalue.

1. Do constants constitute RValues? const int x = 0; is maniputable at least one time.

No, because you an do this - const int *p = &x

—━☆沉默づ 2024-12-29 03:45:13
  1. 所有变量,包括不可修改(const)变量,都是左值。因此 x 是左值,而 0 是右值。

  2. 临时对象被设为右值,因为它们在特定语句之外不存在。

您可以在此处阅读更多信息

  1. All variables, including nonmodifiable (const) variables, are lvalues. So x is an lvalue whereas 0 is an rvalue.

  2. Temporary objects are made rvalues since they do not exist beyond that particular statement.

You can read more here

毁虫ゝ 2024-12-29 03:45:13

RValue 是不可操作的内存区域

右值是表达式,而不是“事物”。

右值可以引用对象(“可操作的内存区域”)。

因此像整数这样的文字被视为 RValue。

文字是右值(不是“被视为”右值)因为 C++ 就是这样定义的

常量构成 RValue 吗?

const int x = 0;

实际上,x 是一个 const int 类型的变量

给定上述 x 的定义,表达式 x 是左值。

现在,编译器创建的临时对象也是RValue

不,右值是表达式,不是对象

即使它们具有可操作的内存区域。为什么会这样?

事实并非如此。

因为它们不能被“用户”修改?是这个原因吗?

右值是右值,因为语言是这样定义的。

那么,“用户”不可操作的内存区域称为 RValue?

不,右值仅指定表达式,而不是像内存这样运行时存在的东西。

右值有时可以引用一个对象,它是用户可以操作的“内存区域”。

“用户”不可操作的内存区域

不清楚您在这里的意思;你的意思是只读存储器,还是其他什么?

RValues are things which are not maniputable regions of memory

rvalues are expressions, not "things".

rvalues can refer to objects ("maniputable regions of memory").

so literals like integers are considered RValues.

Literals are rvalue (not "considered" rvalues) because C++ is defined that way.

Do constants constitute RValues?

const int x = 0;

Actually, x is a variable of type const int.

Given the above definition of x, the expression x is a lvalue.

Now, the temporary objects created by the compiler are also RValues

No, rvalues are expressions, not objects.

even when they have maniputable memory regions. Why is that so?

It isn't so.

Because they cannot be modified by "users"? Is this the reason?

rvalues are rvalues because the language is defined this way.

So, a memory region which is NOT maniputable by the "users" is called RValue?

No, rvalue only designate expressions, not things that exist at runtime like memory.

A rvalue can sometimes refer to an object, which is a "memory region" that the user can manipulate.

memory region which is NOT maniputable by the "users"

It isn't clear what you mean here; do you mean read-only memory, or something else?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文